`
yanshaozhi
  • 浏览: 102866 次
  • 性别: Icon_minigender_1
  • 来自: 东营
社区版块
存档分类
最新评论

使用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[4096]; // 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[i]);
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();
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics