我有一个压缩文件,这是在一个压缩文件的文件夹内,请建议我如何使用压缩输入流读取它。
例如:
abc.zip
|
documents/bcd.zip
如何读取zip文件里面的zip文件?
发布于 2016-11-11 15:30:39
如果您想要递归地查看zip文件中zip文件,
public void lookupSomethingInZip(InputStream fileInputStream) throws IOException {
ZipInputStream zipInputStream = new ZipInputStream(fileInputStream);
String entryName = "";
ZipEntry entry = zipInputStream.getNextEntry();
while (entry!=null) {
entryName = entry.getName();
if (entryName.endsWith("zip")) {
//recur if the entry is a zip file
lookupSomethingInZip(zipInputStream);
}
//do other operation with the entries..
entry=zipInputStream.getNextEntry();
}
}
使用从文件派生的文件输入流调用该方法-
File file = new File(name);
lookupSomethingInZip(new FileInputStream(file));
发布于 2012-07-02 12:25:49
下面的代码片段列出了一个ZIP文件在另一个ZIP文件中的条目。使其适应您的需求。ZipFile
在幕后使用ZipInputStream
s。
代码片段使用Apache Commons IO,特别是IOUtils.copy
。
public static void readInnerZipFile(File zipFile, String innerZipFileEntryName) {
ZipFile outerZipFile = null;
File tempFile = null;
FileOutputStream tempOut = null;
ZipFile innerZipFile = null;
try {
outerZipFile = new ZipFile(zipFile);
tempFile = File.createTempFile("tempFile", "zip");
tempOut = new FileOutputStream(tempFile);
IOUtils.copy( //
outerZipFile.getInputStream(new ZipEntry(innerZipFileEntryName)), //
tempOut);
innerZipFile = new ZipFile(tempFile);
Enumeration<? extends ZipEntry> entries = innerZipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
System.out.println(entry);
// InputStream entryIn = innerZipFile.getInputStream(entry);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// Make sure to clean up your I/O streams
try {
if (outerZipFile != null)
outerZipFile.close();
} catch (IOException e) {
e.printStackTrace();
}
IOUtils.closeQuietly(tempOut);
if (tempFile != null && !tempFile.delete()) {
System.out.println("Could not delete " + tempFile);
}
try {
if (innerZipFile != null)
innerZipFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
readInnerZipFile(new File("abc.zip"), "documents/bcd.zip");
}
发布于 2019-02-25 15:43:23
最终让它修复了Manas Maji的答案。最小解决方案:
import java.io.*;
import java.nio.file.*;
import java.util.zip.*;
import org.slf4j.*;
public void readZipFileRecursive(final Path zipFile) {
try (final InputStream zipFileStream = Files.newInputStream(zipFile)) {
this.readZipFileStream(zipFileStream);
} catch (IOException e) {
LOG.error("error reading zip file %s!", zipFile, e);
}
}
private void readZipFileStream(final InputStream zipFileStream) {
final ZipInputStream zipInputStream = new ZipInputStream(zipFileStream);
ZipEntry zipEntry;
try {
while ((zipEntry = zipInputStream.getNextEntry()) != null) {
LOG.info("name of zip entry: {}", zipEntry.getName());
if (!zipEntry.isDirectory() && zipEntry.getName().endsWith(".zip")) {
this.readZipFileStream(zipInputStream); // recursion
}
}
} catch (IOException e) {
LOG.error("error reading zip file stream", e);
}
}
注意:不要在递归方法中关闭流。
https://stackoverflow.com/questions/11287486
复制相似问题