62,635
社区成员




zip = new ZipOutputStream(new ZipOutputStream(new FileOutputStream(
Zfile)));
zip = new ZipOutputStream(new FileOutputStream(Zfile));
public class FieToZip {
static final int BUFFER = 2048;
static boolean flag = false;
public static File ZipSubdirectory(File myDir) throws IOException {
BufferedInputStream origin = null;
File zipFile = new File("c://"+myDir.getName() + ".zip");
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile));
File dirContents[] = myDir.listFiles();
File tempFile = null;
try {
for (int i = 0; i < dirContents.length; i++) {
if (dirContents[i].isDirectory()) {
tempFile = ZipSubdirectory(dirContents[i]);
flag = true;
} else {
tempFile = dirContents[i];
flag = false;
}
FileInputStream fis = new FileInputStream(tempFile);
origin = new BufferedInputStream(fis, BUFFER);
ZipEntry entry = new org.apache.tools.zip.ZipEntry(tempFile
.getName());
byte data[] = new byte[BUFFER];
int count;
out.putNextEntry(entry);
while ((count = origin.read(data, 0, BUFFER)) != -1) {
out.write(data, 0, count);
}
out.closeEntry();
origin.close();
if (flag == true) {
flag = tempFile.delete();
}
}
} catch (Exception e) {
System.out.println(e);
}
out.close();
return zipFile;
}
}