gpt4 book ai didi

java - 在内存中解压缩 *.docx 文件而不写入磁盘 - Java

转载 作者:行者123 更新时间:2023-12-01 11:29:03 28 4
gpt4 key购买 nike

我想将 *.docx 文件解压缩到内存中,而不将输出写入磁盘。我找到了以下实现,但它只允许读取压缩文件,而不能查看目录结构。了解目录树中每个文件的位置对我来说很重要。有人可以给我指点吗?

private static void UnzipFileInMemory() {
try {
ZipFile zf = new ZipFile("d:\\a.docx");

int i = 0;
for (Enumeration e = zf.entries(); e.hasMoreElements();) {
InputStream in = null;
try {
ZipEntry entry = (ZipEntry) e.nextElement();
System.out.println(entry);
in = zf.getInputStream(entry);
} catch (IOException ex) {
//Logger.getLogger(Tester.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
in.close();
} catch (IOException ex) {
//Logger.getLogger(Tester.class.getName()).log(Level.SEVERE, null, ex);
}
}

}
} catch (IOException ex) {
//Logger.getLogger(Tester.class.getName()).log(Level.SEVERE, null, ex);
}
}

最佳答案

使用 ZipInputStream :本例中的 zEntry 为您提供文件位置。

import java.io.BufferedInputStream;
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 unzip {

public static void main(String[] args) {

String filePath = "D:/Tmp/Tmp.zip";
String oPath = "D:/Tmp/";

new unzip().unzipFile(filePath, oPath);
}

public void unzipFile(String filePath, String oPath) {

FileInputStream fis = null;
ZipInputStream zipIs = null;
ZipEntry zEntry = null;
try {
fis = new FileInputStream(filePath);
zipIs = new ZipInputStream(new BufferedInputStream(fis));
while ((zEntry = zipIs.getNextEntry()) != null) {
try {
FileOutputStream fos = null;
String opFilePath = oPath + zEntry.getName();
fos = new FileOutputStream(opFilePath);
System.out.println(zEntry.getName());

fos.flush();
fos.close();
} catch (Exception ex) {

}
}
zipIs.close();
fis.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

关于java - 在内存中解压缩 *.docx 文件而不写入磁盘 - Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30572523/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com