我有一个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中。
答案 0 :(得分:0)
这是遍历给定ZIP文件并在其中打印每个文件的前16个字节的代码。
{
"count": 1,
"results": [
{
"birthDate": "1990-07-11",
"cardNumber": "0QBD0040267646",
"cellPhone": "(514) 333-4444",
"city": "Brossard",
"email": "farouk.rahem@bciti.com",
}
],
"start": 0,
"success": True,
"total": 1
}
答案 1 :(得分:-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();
}
}