package test;
import java.io.*;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ZipAddOutputStream3 {
public static void main(String[] args) throws IOException {
File file = new File("d:" + File.separator + "demo.txt");
File zipFile = new File("d:" + File.separator + "demo.zip");
InputStream input = null;
ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(
zipFile));
int len = 0;
if (file.isDirectory()) {
yaSuo(file, input, zipOut);
} else {
input = new FileInputStream(file);
zipOut.putNextEntry(new ZipEntry(file.getName()));
zipOut.setComment(file.getName());
while ((len = input.read()) != -1) {
zipOut.write(len);
}
input.close();
}
zipOut.close();
}
public static void yaSuo(File file, InputStream input,
ZipOutputStream zipOut) throws IOException {
File[] filePath = file.listFiles();
int temp = 0;
for (int i = 0; i < filePath.length; i++) {
if (filePath[i].isDirectory()) {
yaSuo(filePath[i], input, zipOut);
} else {
String path=filePath[i].getAbsolutePath();
int t=path.indexOf("\\");
if(t!=-1)
path=path.substring(t+1, path.length());
System.out.println(path);
input = new FileInputStream(filePath[i]);
zipOut.putNextEntry(new ZipEntry(path));
while ((temp = input.read()) != -1) {
zipOut.write(temp);
}
input.close();
}
}
}
}