如何使用"Zip文件系统提供程序"在Java中遍历ZIP文件?

4

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

根据建议,我决定使用“Zip文件系统提供程序”来处理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());
}

然而,它仅循环一次并返回整个ZIP文件tmp.zip。 我如何逐个提取物理图像文件,以便可以将它们存储在MySQL中?

1
从文件存储开始,而不是文件存储器的根目录开始:for (Path rootDir : fs.getRootDirectories) - Jesper
它返回null。我如何访问文件本身,我已经可以获取目录,这只是用户可以上传的一个。 - Seif
3
只需使用 Files.walk - 这只需要一行代码。您正在遍历底层的存储(即zip),而您需要查看文件系统。例如,可以使用 fs.getPath("/") - Boris the Spider
1
返回什么是 nullfs.getRootDirectories() 会返回 null 吗?那将非常奇怪。 - Jesper
正如@Jesper所说,FileSystem.getRootDirectories()不能返回null - Boris the Spider
显示剩余3条评论
2个回答

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接