public static void main(String[] args) throws IOException {
List<String> fileNames = FileUtil.listFileNames("C:\\Users\\lubingdeng\\Desktop\\归档");
FileOutputStream fileOutputStream = new FileOutputStream("C:\\Users\\lubingdeng\\Desktop\\files.zip");
ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream);
for (String fileName : fileNames) {
FileInputStream in = new FileInputStream("C:\\Users\\lubingdeng\\Desktop\\归档\\"+fileName);
ZipEntry testOk = new ZipEntry(fileName);
zipOutputStream.putNextEntry(testOk);
int len;
byte[] buf = new byte[2048];
while ((len = in.read(buf)) > 0) {
zipOutputStream.write(buf, 0, len);
}
zipOutputStream.closeEntry();
in.close();
}
zipOutputStream.close();
fileOutputStream.close();
}
文件压缩demo
最新推荐文章于 2024-05-06 10:37:06 发布