public static boolean zipDirectory(File directory) {
ZipOutputStream out = null;
BufferedInputStream in = null;
try {
File zippedDirectory = new File(directory.getParent()
+ File.separatorChar + directory.getName() + ".zip");
out = new ZipOutputStream(new BufferedOutputStream(
new FileOutputStream(zippedDirectory)));
byte[] data = new byte[1000];
String files[] = directory.list();
for (int i = 0; i < files.length; i++) {
final FileInputStream fileInputStream = new FileInputStream(directory
.getPath()
+ File.separatorChar + files[i]);
in = new BufferedInputStream(fileInputStream, 1000);
out.putNextEntry(new ZipEntry(files[i]));
int count;
while ((count = in.read(data, 0, 1000)) != -1) {
out.write(data, 0, count);
}
out.closeEntry();
fileInputStream.close();
}
return true;
} catch (Exception ex) {
ex.printStackTrace();
return false;
} finally {
try {
in.close();
out.flush();
out.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
Monday, June 14, 2010
How to zip a directory in java
This is a sample method To zip a directory in java :
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment