簡體   English   中英

如何使用“ Zip文件系統提供程序”遍歷Java中的ZIP文件?

[英]How to traverse a ZIP file in Java using “Zip File System Provider”?

我有一個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());
}

但是,它僅循環一次並返回tmp.zip ,即整個ZIP。 如何一一提取物理圖像文件,以便將其存儲在MySQL中。

這是遍歷給定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();
    });
}

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM