简体   繁体   English

如何使用Java对文件夹及其所有文件和子目录进行zip / upzip?

[英]How to zip/upzip a folder and all of its files and subdirectories using Java?

I was reading this to learn how to zip/unzip files using Java. 我正在阅读本文,以了解如何使用Java压缩/解压缩文件。 I used this to guide me and it worked great when zipping all the files inside a folder, but when I tested it with a folder containing more folders inside of it, it didn't work, it threw the following error: 我用来指导我,将所有文件压缩到一个文件夹中时效果很好,但是当我使用其中包含更多文件夹的文件夹对其进行测试时,它不起作用,它引发了以下错误:

java.io.FileNotFoundException: assets (Access is denied) //assets is the name of the folder I tried to zip
        at java.io.FileInputStream.open(Native Method)
        at java.io.FileInputStream.<init>(Unknown Source)
        at java.io.FileInputStream.<init>(Unknown Source)
        at Zip.main(Zip.java:24)

This is the class I'm using, as you will see it's the same Code Sample 4: Zip.java class code from the previous link: 这是我正在使用的类,您将看到它是相同的代码示例4:上一个链接中的Zip.java类代码:

import java.io.*;
import java.util.zip.*;

public class Zip {
   static final int BUFFER = 2048;
   public void zip() {
      try {
         BufferedInputStream origin = null;
         FileOutputStream dest = new 
           FileOutputStream("H:\\myfigs.zip");
         CheckedOutputStream checksum = new 
           CheckedOutputStream(dest, new Adler32());
         ZipOutputStream out = new 
           ZipOutputStream(new 
             BufferedOutputStream(checksum));
         //out.setMethod(ZipOutputStream.DEFLATED);
         byte data[] = new byte[BUFFER];
         // get a list of files from current directory
         File f = new File(".");
         String files[] = f.list();

         for (int i=0; i<files.length; i++) {
            System.out.println("Adding: "+files[i]);
            FileInputStream fi = new 
              FileInputStream(files[i]);
            origin = new 
              BufferedInputStream(fi, BUFFER);
            ZipEntry entry = new ZipEntry(files[i]);
            out.putNextEntry(entry);
            int count;
            while((count = origin.read(data, 0, 
              BUFFER)) != -1) {
               out.write(data, 0, count);
            }
            origin.close();
         }
         out.close();
         System.out.println("checksum: "+checksum.getChecksum().getValue());
      } catch(Exception e) {
         e.printStackTrace();
      }
   }
} 

What changes should be made so this code can zip folders inside folder and all of its files into a zip file? 应该进行哪些更改,以便此代码可以将文件夹内的文件夹及其所有文件压缩为zip文件?

The zip entry needs to specify the path of the file inside the archive. zip条目需要指定档案内部文件的路径。 You can't add a folder to a zip archive - you can only add the files within the folder. 您不能将文件夹添加到zip存档中-您只能在该文件夹中添加文件。

The naming convention is to use forward slashes as the path separator. 命名约定是使用正斜杠作为路径分隔符。 If you are zipping a folder with the following files/subdirectories: 如果要使用以下文件/子目录压缩文件夹:

c:\foo\bar\a.txt
c:\foo\bar\sub1\b.txt
c:\foo\bar\sub2\c.txt

the zip entry names would be: zip条目名称为:

a.txt
sub1/b.txt
sub2/c.txt

So to fix your algorithm, add isDirectory() inside your for loop, and then recursively add the files in any subdirectory to the zip. 因此,要修复算法,请在for循环内添加isDirectory(),然后以递归方式将任何子目录中的文件添加到zip中。 Probably the best way to do this is to have a method: 可能最好的方法是有一个方法:

addDirectoryToZip(String prefix, File directory, ZipOutputStream out)

Here's a solution for the problem: java.util.zip - Recreating directory structure 这是该问题的解决方案: java.util.zip-重新创建目录结构

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Java代码将文件夹(包括子目录和文件)移动到新文件夹中 - How to move a folder(including subdirectories and files) into a new folder using Java code 使用webkitDirectory和Java Servlet在文件夹/子目录中上传文件 - Upload files in folder/subdirectories using webkitDirectory and Java Servlet 如何在hadoop hdfs中列出目录及其子目录中的所有文件 - How to list all files in a directory and its subdirectories in hadoop hdfs 如何解压缩 Java 目录中的所有 Zip 文件夹? - How to Unzip all the Zip folder in an directory in Java? 如何使用Java获取目录及其子目录中多个文件的相同xml元素值? - How to get the same xml element value of multiple files in a directories and its subdirectories using java? 快速列出目录及其所有子目录中的文件 - Fast listing files in a directory and all of its subdirectories 如何使用 Java 解压缩目录中所有受密码保护的 zip 文件 - How to unzip all the password protected zip files in a directory using Java 如何使用java压缩文件夹本身 - how to zip a folder itself using java 如何将文件夹及其子目录上传到Dropbox帐户? - How to upload a folder and its subdirectories to a Dropbox account? 使用JAVA将包含文件的文件夹添加到zip文件中 - Add a folder having files into a zip file using JAVA
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM