java 如何在java中提取zip文件中的特定文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32179094/
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-11-02 19:46:56  来源:igfitidea点击:

How to extract specific file in a zip file in java

javazip

提问by Mr rain

I need to provide a view of zip file to customer in system, and allow customers download choosed files.

我需要在系统中向客户提供 zip 文件的视图,并允许客户下载所选文件。

  1. parse the zip file and show on the web page. and remember every zipentry location(for example file1 is starting from byte 100 width length 1024bytes) in backend.
  2. download the specified file when customer click the download button.
  1. 解析 zip 文件并显示在网页上。并记住后端的每个 zipentry 位置(例如 file1 从字节 100 宽度长度 1024bytes 开始)。
  2. 当客户点击下载按钮时,下载指定的文件。

now I have rememberred all zipentry locations, but is there java zip tools to unzip the specified location of zip files?? API just like unzip(file, long entryStart, long entryLength);

现在我已经记住了所有的 zipentry 位置,但是有没有 java zip 工具来解压 zip 文件的指定位置?API 就像 unzip(file, long entryStart, long entryLength);

回答by Thunderforge

This can be done without messing with byte arrays or input streams using Java 7's NIO2:

这可以使用 Java 7 的 NIO2 来完成,而不会弄乱字节数组或输入流:

public void extractFile(Path zipFile, String fileName, Path outputFile) throws IOException {
    // Wrap the file system in a try-with-resources statement
    // to auto-close it when finished and prevent a memory leak
    try (FileSystem fileSystem = FileSystems.newFileSystem(zipFile, null)) {
        Path fileToExtract = fileSystem.getPath(fileName);
        Files.copy(fileToExtract, outputFile);
    }
}

回答by Amit Bhati

You can use the below code to extract a particular file from zip:-

您可以使用以下代码从 zip 中提取特定文件:-

public static void main(String[] args) throws Exception{
        String fileToBeExtracted="fileName";
        String zipPackage="zip_name_with_full_path";
        OutputStream out = new FileOutputStream(fileToBeExtracted);
        FileInputStream fileInputStream = new FileInputStream(zipPackage);
        BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream );
        ZipInputStream zin = new ZipInputStream(bufferedInputStream);
        ZipEntry ze = null;
        while ((ze = zin.getNextEntry()) != null) {
            if (ze.getName().equals(fileToBeExtracted)) {
                byte[] buffer = new byte[9000];
                int len;
                while ((len = zin.read(buffer)) != -1) {
                    out.write(buffer, 0, len);
                }
                out.close();
                break;
            }
        }
        zin.close();

    }

Also refer this link: How to extract a single file from a remote archive file?

另请参阅此链接:如何从远程存档文件中提取单个文件?

回答by Garry

You can try like this:

你可以这样试试:

ZipFile zf = new ZipFile(file);
try {
  InputStream in = zf.getInputStream(zf.getEntry("file.txt"));
  // ... read from 'in' as normal
} finally {
  zf.close();
}

I havent tried it but in Java 7 ZipFileSystem you can try like this to extract file.TXT file from the zip file.

我还没有尝试过,但是在 Java 7 ZipFileSystem 中,您可以尝试这样从 zip 文件中提取 file.TXT 文件。

Path zipfile = Paths.get("/samples/ziptest.zip");
FileSystem fs = FileSystems.newFileSystem(zipfile, env, null);
final Path root = fs.getPath("/file.TXT");