如何在java中的zip文件中提取特定文件 [英] How to extract specific file in a zip file in java
本文介绍了如何在java中的zip文件中提取特定文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在系统中向客户提供zip文件视图,并允许客户下载所选文件。
I need to provide a view of zip file to customer in system, and allow customers download choosed files.
- 解析zip文件并显示在网页上。并记住后端的每个zipentry位置(例如file1从字节100宽度长度1024字节开始)。
- 在客户点击下载按钮时下载指定的文件。
现在我已经记住了所有的zipentry位置,但有没有java zip工具来解压缩zip文件的指定位置?
API就像unzip(file,long entryStart,long entryLength);
now I have rememberred all zipentry locations, but is there java zip tools to unzip the specified location of zip files?? API just like unzip(file, long entryStart, long entryLength);
推荐答案
你可以使用下面的代码来从zip中提取特定文件: -
You can use the below code to extract a particular file from zip:-
public static void main(String[] args) throws Exception{
String fileToBeExtracted="fileName";
String zipPackage="zip_name_with_full_path";
OutputStream out = new FileOutputStream(fileToBeExtracted);
FileInputStream fileInputStream = new FileInputStream(zipPackage);
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream );
ZipInputStream zin = new ZipInputStream(bufferedInputStream);
ZipEntry ze = null;
while ((ze = zin.getNextEntry()) != null) {
if (ze.getName().equals(fileToBeExtracted)) {
byte[] buffer = new byte[9000];
int len;
while ((len = zin.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
out.close();
break;
}
}
zin.close();
}
另请参阅此链接:如何从远程存档文件中提取单个文件?
这篇关于如何在java中的zip文件中提取特定文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文