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

时间:2012-09-24 09:47:24

标签: java zip

我希望用Java创建一个zip程序,其zip文件和文件夹就像这样的结构 -

  • folder-one /
  • 夹酮/此时就把one.txt存盘
  • 夹酮/ two.mp3
  • 夹酮/ three.jpg
  • 夹个/
  • 夹个/ four.doc
  • 夹个/ five.rtf
  • folder-two / folder-three /
  • 夹个/文件夹三/ six.txt

我使用了zip4j开源,我已经将所有文件(带有绝对路径)收集在一个列表中,然后将其分配给zip,但它只是在my.zip中压缩文件 -

  • 此时就把one.txt存盘
  • two.mp3
  • three.jpg
  • four.doc
  • five.rtf
  • six.txt

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

代码如下 -

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");
    }
}

谢谢!

1 个答案:

答案 0 :(得分:0)

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

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

请仔细阅读以下链接,您会找到代码段并详细说明。

如果有帮助,请告诉我!

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