Here you can find the source of zipDirectory(File root, File directory, ZipOutputStream zos)
Parameter | Description |
---|---|
root | the root |
directory | the directory |
zos | the zos |
Parameter | Description |
---|---|
Exception | the exception |
static void zipDirectory(File root, File directory, ZipOutputStream zos) throws Exception
//package com.java2s; import java.io.File; import java.io.FileInputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class Main { /**//w ww .j ava2 s . c om * Zip directory. * * @param root the root * @param directory the directory * @param zos the zos * @throws Exception the exception */ static void zipDirectory(File root, File directory, ZipOutputStream zos) throws Exception { for (File item : directory.listFiles()) { if (item.isDirectory()) { zipDirectory(root, item, zos); } else { byte[] readBuffer = new byte[2156]; int bytesIn; FileInputStream fis = new FileInputStream(item); String path = item.getAbsolutePath().substring(root.getAbsolutePath().length() + 1); ZipEntry anEntry = new ZipEntry(path); zos.putNextEntry(anEntry); while ((bytesIn = fis.read(readBuffer)) != -1) { zos.write(readBuffer, 0, bytesIn); } fis.close(); } } } }