微信

使用微信服务,更方便

职友集>Java面试题 > 写出java解压缩zip文件代码

写出java解压缩zip文件代码

2015-08-06 06:30:02 阅读( 260 )

2567人 收藏本页

标签:Java面试题

下面示例代码演示解压缩zip文件,该zip文件只包含一个文件:

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

/**
*
* @author outofmemory.cn
*/
public class Main {

/**
* 解压zip文件
*/
public void extractZipFile() {

try {
String zipFileName = "compressed.zip";
String extractedFileName = "extracted.txt";

//Create input and output streams
ZipInputStream inStream = new ZipInputStream(new FileInputStream(zipFileName));
OutputStream outStream = new FileOutputStream(extractedFileName);

ZipEntry entry;
byte[] buffer = new byte[1024];
int nrBytesRead;

//Get next zip entry and start reading data
if ((entry = inStream.getNextEntry()) != null) {
while ((nrBytesRead = inStream.read(buffer)) > 0) {
outStream.write(buffer, 0, nrBytesRead);
}
}

//Finish off by closing the streams
outStream.close();
inStream.close();

} catch (IOException ex) {
ex.printStackTrace();
}
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
new Main().extractZipFile();
}

}

下一篇:java使用Message Handler修改WebService客户端的SOAP头

上一篇:java列出文件目录结构

亲~ 如果您有更好的答案 可在评论区发表您独到的见解。

您想查看更多的信息: 面试题