zip4j 压缩 java 中的文件和文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12562519/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 09:22:49  来源:igfitidea点击:

zip4j zipping for files and folders in java

javazip

提问by Neelam Sharma

I wish to create a zip program in Java, which zip files and folders let say structure like this -

我希望在 Java 中创建一个 zip 程序,其中 zip 文件和文件夹可以这样说结构 -

  • folder-one/
  • folder-one/one.txt
  • folder-one/two.mp3
  • folder-one/three.jpg
  • folder-two/
  • folder-two/four.doc
  • folder-two/five.rtf
  • folder-two/folder-three/
  • folder-two/folder-three/six.txt
  • 文件夹一/
  • 文件夹一/一.txt
  • 文件夹一/二.mp3
  • 文件夹一/三.jpg
  • 文件夹二/
  • 文件夹二/四.doc
  • 文件夹二/五.rtf
  • 文件夹二/文件夹三/
  • 文件夹二/文件夹三/六.txt

I used zip4j open source, I have collected all the files (with absolute path) in one list then given it to zip but it is zipping files only as in my.zip -

我使用了 zip4j 开源,我在一个列表中收集了所有文件(带绝对路径),然后将其压缩,但它仅像在 my.zip 中一样压缩文件 -

  • one.txt
  • two.mp3
  • three.jpg
  • four.doc
  • five.rtf
  • six.txt
  • 一个.txt
  • 二.mp3
  • 三.jpg
  • 四.doc
  • 五.rtf
  • 六.txt

How can I preserve same structure on zipping and unzipping as it was on local earlier. Please suggest if any other open source can help me to zip/unzip in same structure files and folders like other windows zip programs.

我如何在压缩和解压缩时保留与之前在本地相同的结构。请建议是否有任何其他开源可以帮助我像其他 Windows zip 程序一样压缩/解压缩相同结构的文件和文件夹。

Code is below --

代码如下——

public class CreateZipWithOutputStreams {

    ArrayList filesToAdd = new ArrayList();

    public void CreateZipWithOutputStreams(String sAbsolutePath) {


        ZipOutputStream outputStream = null;
        InputStream inputStream = null;

        try {

            ArrayList arrLocal = exploredFolder(sAbsolutePath);


            outputStream = new ZipOutputStream(new FileOutputStream(new File("c:\ZipTest\CreateZipFileWithOutputStreams.zip")));

            ZipParameters parameters = new ZipParameters();

            parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);


            parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);

            parameters.setEncryptFiles(true);

            parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);


            parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);


            parameters.setPassword("neelam");


            for (int i = 0; i < arrLocal.size(); i++) {
                File file = (File) arrLocal.get(i);


                outputStream.putNextEntry(file, parameters);

                if (file.isDirectory()) {
                    outputStream.closeEntry();
                    continue;
                }


                inputStream = new FileInputStream(file);
                byte[] readBuff = new byte[4096];
                int readLen = -1;

                while ((readLen = inputStream.read(readBuff)) != -1) {
                    outputStream.write(readBuff, 0, readLen);
                }

                outputStream.closeEntry();

                inputStream.close();
            }

             outputStream.finish();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public ArrayList exploredFolder(String sAbsolutePath) {
        File[] sfiles;
        File fsSelectedPath = new File(sAbsolutePath);
        sfiles = fsSelectedPath.listFiles();
        if (sfiles == null) {
            return null;
        }
        for (int j = 0; j < sfiles.length; j++) {
            File f = sfiles[j];
            if (f.isDirectory() == true) {
                exploredFolder(f.getAbsolutePath());
            } else {
                filesToAdd.add(f);

            }
        }
        return filesToAdd;
    }


    public static void main(String[] args) {
        new CreateZipWithOutputStreams().CreateZipWithOutputStreams("c:\ZipTest");
    }
}

Thanks!

谢谢!

采纳答案by Som Bhattacharyya

Okay so first the code that is attached is supposed to work the way it is because the exploredFolder(String absolutePath)method is returning the "files to add" which in turn is being used by the CreateZipWithOutputStreams()method to create a single layered(flat) zip file.

好的,首先附加的代码应该按照它的方式工作,因为该exploredFolder(String absolutePath)方法正在返回“要添加的文件”,而该CreateZipWithOutputStreams()方法又使用它来创建单层(平面)zip 文件。

What needs to be done is looping over the individual folders and keep adding them to the ZipOutputStream.

需要做的是循环遍历各个文件夹并继续将它们添加到ZipOutputStream.

Please go through the link below and you will find the code snippet and detailed explaination.

请通过下面的链接,您将找到代码片段和详细说明。

Let me know if that helps!

如果有帮助,请告诉我!

http://www.java-forums.org/blogs/java-io/973-how-work-zip-files-java.html

http://www.java-forums.org/blogs/java-io/973-how-work-zip-files-java.html