压缩包含子文件夹的文件夹
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)
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();
}
}
I'm trying to zip a folder which contains subfolders. Trying to zip the folder named 10-18-2010_4D.The above program ends with the following exception. Please advise on how to clear the issue.
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
这是我写的。
此示例保留了文件的结构,从而避免了重复条目异常。
Here is what I've written.
This example keeps the structure of the files and by that, avoid the duplicate entry exception.
这是我在压缩一段时间后想到的另一个例子。它与其他示例类似,但我在我认为需要更多解释的那些区域添加了很多注释。 Java SE9
Here is another example that I came up with after struggeling with zipping for some time. It's similar to the other examples but I added a lot of comments in those areas where I thought more explanation was needed. Java SE9
Java 7+ 版本,使用 Path、FileVisitor 和 AutoCloseable 接口。要使用此示例,只需调用 zipWalking(sourceDir,targetZipFile);
Java 7+ version, using Path, FileVisitor and AutoCloseable interfaces. To use this example, just call
zipWalking(sourceDir,targetZipFile);
下面的代码维护 zip 文件内 sourceFolder 的目录结构。它还可以遍历到 Integer.MAX_VALUE 的深度。
我发现使用 Files.walkFileTree(..) 比 Files.walk(..) 更简单、更干净
驱动程序代码:
createZipOfFolder("结果/文件夹")
输出:
results/folder.zip
实施:
The below code maintains the directory structure of the sourceFolder inside zip file. Also it can traverse upto depth of Integer.MAX_VALUE.
I have found that using Files.walkFileTree(..) is much simpler and cleaner than Files.walk(..)
Driver code:
createZipOfFolder("results/folder")
Output:
results/folder.zip
Implementation:
以下是创建 ZIP 存档的代码。创建的存档保留原始目录结构(如果有)。
调用此方法后不要忘记关闭输出流。这是例子:
Here's the code for creating the ZIP archive. Created archive preserves original directory structure (if any).
Don't forget to close output streams after calling this method. Here's the example:
您只需使用此库Zeroturnaround Zip 库
然后你将你的文件夹压缩为一行:
Simply you can use this library Zeroturnaround Zip library
Then you will zip your folder just a one line:
您需要检查文件是否是目录,因为您无法将目录传递给 zip 方法。
查看此页面,其中显示了如何递归压缩给定目录。
You need to check if the file is a directory because you can't pass directories to the zip method.
Take a look at this page which shows how you can recursively zip a given directory.
我将包含 用于压缩的 ant 任务 - 它更容易使用。
任务类可以在这里找到:
org.apache.tools.ant.taskdefs.Zip
(以编程方式使用它)I would include the ant task for zipping - it is way easier to work with.
The task class can be found here:
org.apache.tools.ant.taskdefs.Zip
(use it programatically)