yanshaozhi 发表于 2013-2-3 10:17:23

使用java打包为zip文件

public void zipFile() throws IOException {
String dir="e:/testPicture";
File d = new File(dir);
if (!d.isDirectory())
throw new IllegalArgumentException("Not a directory:" + dir);
String[] entries = d.list();
byte[] buffer = new byte; // Create a buffer for copying
int bytesRead;
//File zipfile=new File("e:test.zip");
ZipOutputStream out = new ZipOutputStream(new FileOutputStream("e:/test.zip"));

for (int i = 0; i < entries.length; i++) {
File f = new File(d, entries);
if (f.isDirectory())
continue;
FileInputStream in = new FileInputStream(f);
ZipEntry entry = new ZipEntry(f.getPath());
out.putNextEntry(entry);
while ((bytesRead = in.read(buffer)) != -1)
out.write(buffer, 0, bytesRead);
in.close();
}
out.close();
}
页: [1]
查看完整版本: 使用java打包为zip文件