如何使用“Zip文件系统提供程序”在Java中遍历ZIP文件? [英] How to traverse a ZIP file in Java using "Zip File System Provider"?

查看:216
本文介绍了如何使用“Zip文件系统提供程序”在Java中遍历ZIP文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



建议我决定使用Zip文件系统提供者来处理ZIP文件:

 路径zipPath = Paths.get zipFile.getSubmittedFileName()); //返回ZIP文件的路径
FileSystem fs = FileSystems.newFileSystem(zipPath,null); //创建文件系统

我试着用下面的方法遍历它:

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





$ b

但是它只循环一次,返回 tmp。 zip 这是整个ZIP。我怎么提取物理图像文件一个接一个,所以我可以将它们存储在MySQL中。 Apache Commons Compress 模块可能可以帮助您遍历文件。



下面是一个可以遍历多个文件并提取字节内容的样本提取文件
Sample


  / * 
*要更改此许可证标题,请在项目属性中选择许可证标题。
*要更改此模板文件,请选择工具|模板
*并在编辑器中打开模板。
* /
包测试;
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;
字符串destinationDir =C:\\temp\\\;
ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(fileName));
ZipEntry zipEntry = zipInputStream.getNextEntry();
byte [] buffer = new byte [1024];
while(zipEntry!= null){
String zipFileName = zipEntry.getName();
文件extractFile =新文件(destinationDir + File.separator + zipFileName);
新文件(extractedFile.getParent())。mkdirs();
FileOutputStream fos = new FileOutputStream(extractedFile);
int len; ((len = zipInputStream.read(buffer))> 0){
fos.write(buffer,0,len);
}
fos.close();
zipEntry = zipInputStream.getNextEntry();
}
zipInputStream.closeEntry();
zipInputStream.close();
}
}


I have a JSP application that allows the user to upload a ZIP file and then the application will read all the files in the ZIP and store them in a MySQL.

Upon advice I decided to use "Zip File System Provider" to handle the ZIP file:

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

I tried to traverse it using:

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

However it loops only one time and returns tmp.zipwhich is the entire ZIP. How do I extract the physical image files one by one so I can store them in MySQL.

解决方案

The Apache Commons Compress module probably can help you to iterate through the files.

Below is a sample extract that can iterate over multiple files and extract the byte contents Sample

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

这篇关于如何使用“Zip文件系统提供程序”在Java中遍历ZIP文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆