public static void main(String argv[]) {
try {
String date = new java.text.SimpleDateFormat("MM-dd-yyyy")
.format(new java.util.Date());
File inFolder = new File("Output/" + date + "_4D");
File outFolder = new File("Output/" + date + "_4D" + ".zip");
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(
new FileOutputStream(outFolder)));
BufferedInputStream in = null;
byte[] data = new byte[1000];
String files[] = inFolder.list();
for (int i = 0; i < files.length; i++) {
in = new BufferedInputStream(new FileInputStream(
inFolder.getPath() + "/" + files[i]), 1000);
out.putNextEntry(new ZipEntry(files[i]));
int count;
while ((count = in.read(data, 0, 1000)) != -1) {
out.write(data, 0, count);
}
out.closeEntry();
}
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
我正在尝试压缩一个包含子文件夹的文件夹。尝试压缩名为 10-18-2010_4D 的文件夹。上述程序以以下异常结束。请告知如何解决问题。
java.io.FileNotFoundException: Output\10-18-2010_4D\4D (Access is denied)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at ZipFile.main(ZipFile.java:17)
原文由 LGAP 发布,翻译遵循 CC BY-SA 4.0 许可协议
您需要检查文件是否为目录,因为您无法将目录传递给 zip 方法。
查看 此页面,其中显示了如何递归压缩给定目录。