【问题标题】:How to traverse a ZIP file in Java using "Zip File System Provider"?如何使用“Zip 文件系统提供程序”在 Java 中遍历 ZIP 文件?
【发布时间】:2016-12-18 01:56:14
【问题描述】:

我有一个 JSP 应用程序,它允许用户上传 ZIP 文件,然后该应用程序将读取 ZIP 中的所有文件并将它们存储在 MySQL 中。

根据建议,我决定使用“Zip File System Provider”来处理 ZIP 文件:

Path zipPath = Paths.get(zipFile.getSubmittedFileName());//returns the path to the ZIP file
FileSystem fs = FileSystems.newFileSystem(zipPath, null);//creates the file system

我尝试使用以下方法遍历它:

for (FileStore store: fs.getFileStores()) {
         System.err.println("Store:  " + store.name());
}

但是它只循环一次并返回tmp.zip这是整个ZIP。如何将物理图像文件一个一个提取出来,以便将它们存储在 MySQL 中。

【问题讨论】:

  • FileSystem的根目录开始,而不是文件存储:for (Path rootDir : fs.getRootDirectories)
  • 它返回空值。自己怎么访问文件,已经可以拿到目录了,只有用户可以上传的一个。
  • 只需使用Files.walk - 大约是 1 行代码。您正在遍历底层的 store,这当然是 zip。您需要查看文件系统。以fs.getPath("/") 为例。
  • 什么返回null; fs.getRootDirectories() 是否返回 null?那真的很奇怪。
  • 正如@Jesper 所说,FileSystem.getRootDirectories() 不能返回 null

标签: java jsp filesystems zip


【解决方案1】:

这是遍历给定 ZIP 文件并打印每个文件的前 16 个字节的代码。

Path filePath = Paths.get("somefile.zip");
FileSystem fileSystem = FileSystems.newFileSystem(filePath, null);
byte[] buffer = new byte[16];
Base64.Encoder encoder = Base64.getEncoder();
for (Path rootDirectory : fileSystem.getRootDirectories()) {
    Files.walk(rootDirectory).forEach(path -> {
        System.out.print(path);
        if (Files.isRegularFile(path)) {
            System.out.print(" ");
            try (InputStream stream = Files.newInputStream(path)) {
                int length = stream.read(buffer);
                for (int i = 0; i < length; i++) {
                    byte b = buffer[i];
                    if (32 <= b && b < 127) {
                        System.out.print((char) b);
                    } else {
                        System.out.printf("\\%02x", b);
                    }
                }
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
        }
        System.out.println();
    });
}

【讨论】:

    【解决方案2】:

    Apache Commons Compress 模块可能可以帮助您遍历文件。

    下面是一个示例提取,它可以遍历多个文件并提取字节内容 样本

    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package test;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipInputStream;
    public class ZipTest {
        public static void main(String[] args) throws FileNotFoundException, IOException {
            String fileName = "C:\\temp\\ECDS-File-Upload-Processed.zip";
            String destinationDir = "C:\\temp\\mango";
            ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(fileName));
            ZipEntry zipEntry = zipInputStream.getNextEntry();
            byte[] buffer = new byte[1024];
            while (zipEntry != null) {
                String zipFileName = zipEntry.getName();
                File extractedFile = new File(destinationDir + File.separator + zipFileName);
                new File(extractedFile.getParent()).mkdirs();
                FileOutputStream fos = new FileOutputStream(extractedFile);
                int len;
                while ((len = zipInputStream.read(buffer)) > 0) {
                    fos.write(buffer, 0, len);
                }
                fos.close();
                zipEntry = zipInputStream.getNextEntry();
            }
            zipInputStream.closeEntry();
            zipInputStream.close();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-15
      • 2011-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-03
      相关资源
      最近更新 更多