java解压缩zip文件代码,java解压缩zip,下面示例代码演示解压缩z
分享于 点击 34651 次 点评:233
java解压缩zip文件代码,java解压缩zip,下面示例代码演示解压缩z
下面示例代码演示解压缩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 byrx.net */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(); }}
用户点评