How to zip a file/directory
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   chander_kumar
Posted On:   Wednesday, July 3, 2002 01:57 AM

I have to attach a directory as a attachment and send a mail,but the directory contains sub-directorys and some files.




if it is possiable to zip the entire directory and send as a attachment in mail.....!




please let me konw some sample code how to zip a directoty/file.

Re: How to zip a file/directory

Posted By:   Gautam_Marwaha  
Posted On:   Wednesday, July 3, 2002 02:09 AM

This method takes 3 args - name of zip file to be created, full directory path, names of the files in specified directory to be zipped.


You can modify this to just pass the first two args and zip ALL files in the specified directory.



After the zip is made you can attach it in your email with javamail



private void zip(String a_strZipFile, String a_strDir, String[] a_fileNames) throws FileNotFoundException, IOException
{
ZipOutputStream zout = new ZipOutputStream( new FileOutputStream(new File(a_strZipFile)) );
byte[] buf = new byte [1024];

for (int i = 0; i < a_fileNames.length; i++)
{
ZipEntry zipEntry = new ZipEntry(a_fileNames[i]);
zout.putNextEntry(zipEntry);

File aFile = new File(a_strDir, a_fileNames[i]);
FileInputStream in = new FileInputStream(aFile);

int bytesRead = in.read(buf, 0, 1024);
while (bytesRead != -1)
{
zout.write(buf, 0, bytesRead);
bytesRead = in.read(buf, 0, 1024);
}
zout.closeEntry();
}
zout.close();
}
About | Sitemap | Contact