Java:使用子文件夹解压缩文件夹

时间:2017-04-10 14:57:39

标签: java

我遇到将文件解压缩到子文件夹的问题。我需要解压缩的zip文件有多个文件夹,我试图在解压缩时保留目录结构。

package components;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

/**
 * This utility extracts files and directories of a standard zip file to
 * a destination directory.
 * 
 *
 */
public class UnzipUtility {
    /**
     * Size of the buffer to read/write data
     */
    private static final int BUFFER_SIZE = 4096;
    /**
     * Extracts a zip file specified by the zipFilePath to a directory specified by
     * destDirectory (will be created if does not exists)
     * @param zipFilePath
     * @param destDirectory
     * @throws IOException
     */
    public void unzip(String zipFilePath, String destDirectory) throws IOException {
        File destDir = new File(destDirectory);
        if (!destDir.exists()) {
            destDir.mkdir();
        }
        ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));

        ZipEntry entry = zipIn.getNextEntry();
        // iterates over entries in the zip file
        while (entry != null) {
            String filePath = destDirectory + File.separator + entry.getName();
            if (!entry.isDirectory()) {
                // if the entry is a file, extracts it
                extractFile(zipIn, filePath);
                System.out.println(filePath);
            } else {
                // if the entry is a directory, make the directory
                File dir = new File(filePath);
                System.out.println(filePath);
                dir.mkdir();
            }
            zipIn.closeEntry();
            entry = zipIn.getNextEntry();
        }
        zipIn.close();
    }
    /**
     * Extracts a zip entry (file entry)
     * @param zipIn
     * @param filePath
     * @throws IOException
     */
    private void extractFile(ZipInputStream zipIn, String filePath) throws IOException {
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
        byte[] bytesIn = new byte[BUFFER_SIZE];
        int read = 0;
        while ((read = zipIn.read(bytesIn)) != -1) {
            bos.write(bytesIn, 0, read);
        }
        bos.close();
    }
}

我从代码中得到的输出如下。问题是文件DoD-DISA-logos-as-JPEG.jpg是子文件夹子文件夹的一部分。所以路径应该是文件夹/子文件夹/子文件夹/ DoD-DISA-logos-as-JPEG.jpg。它不包括那个子文件夹,所以我相信它会得到IO异常。

java.io.FileNotFoundException: /Users/user/Desktop/folder/subfolder/DoD-DISA-logos-as-JPEG.jpg (No such file or directory)
    at java.io.FileOutputStream.open0(Native Method)
    at java.io.FileOutputStream.open(FileOutputStream.java:270)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:213)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:101)
    at components.UnzipUtility.extractFile(UnzipUtility.java:62)
    at components.UnzipUtility.unzip(UnzipUtility.java:42)
    at components.UnzipUtilityTest.main(UnzipUtilityTest.java:9)

1 个答案:

答案 0 :(得分:4)

您不应该依赖entry.isDirectory()ZipEntry迭代器将列出所有文件夹层次结构的事实。

zip文件结构可能只包含一个ZipEntry文件,其名称中包含一个子文件夹层次结构。例如。 foo/bar/file.txt将是ZipEntry但不是foo/也不是foo/bar/

因此:

  • 一个文件,如果File#mkdirs
  • 不存在,您应该获取父目录并创建整个层次结构
  • 一个目录,如果File#mkdirs
  • 不存在,则应创建整个层次结构

您的代码应该转换为类似的内容:

       if (!entry.isDirectory()) {
            // if the entry is a file, extracts it
            new File(filePath).getParentFile().mkdirs();
            extractFile(zipIn, filePath);
            System.out.println(filePath);
        } else {
            // if the entry is a directory, make the directory
            File dir = new File(filePath);
            System.out.println(filePath);
            dir.mkdirs();
        }

我建议使用外部库进行压缩/解压缩,例如zt-zip