java.util.zip
包提供了用于读取和编写zip和gzip文件格式的类。
在这篇文章中,我们将学习如何使用java.util.zip.ZipInputStream
和java.util.zip.ZipFile
解压缩zip文件。
ZipInputStream
– 此类用于按顺序读取zip条目。ZipFile
– 此类在内部使用随机访问文件来读取zip条目。
使用java.util.zip.ZipInputStream
解压缩zip文件
下面是一个演示ZipInputStream
用法的示例。
文件:UnzipFileExample1.java –
package com.yiibai.tutorial.io; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; /** * @author yiibai */ public class UnzipFileExample1 { public static void main(String[] args) { File zipFile = new File("Test.zip"); File output = new File("Unzip"); FileInputStream fileInputStream = null; ZipInputStream zipInputStream = null; try { fileInputStream = new FileInputStream(zipFile); zipInputStream = new ZipInputStream(fileInputStream); // Get ZipEntry ZipEntry entry = zipInputStream.getNextEntry(); while (entry != null) { if (entry.isDirectory()) { // Create directory File dir = new File(output, entry.getName()); if (!dir.exists()) { dir.mkdirs(); } } else { // Read zipEntry and write to a file. File file = new File(output, entry.getName()); FileOutputStream fileOutputStream = new FileOutputStream(file); int i; byte[] data = new byte[1024]; while ((i = zipInputStream.read(data)) != -1) { fileOutputStream.write(data, 0, i); } fileOutputStream.close(); } // Get next entry entry = zipInputStream.getNextEntry(); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (fileInputStream != null) { fileInputStream.close(); } if (zipInputStream != null) { zipInputStream.close(); } } catch (IOException e) { e.printStackTrace(); } } } }
使用java.util.zip.ZipFile解压缩zip文件
以下示例演示了ZipFile的用法。
文件:UnzipFileExample2.java –
package com.yiibai.tutorial.io; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.Enumeration; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; /** * @author yiibai */ public class UnzipFileExample2 { public static void main(String[] args) { File output = new File("Unzip2"); ZipFile zipFile = null; try { zipFile = new ZipFile(new File("Test.zip")); Enumeration<? extends ZipEntry> entries = zipFile.entries(); while (entries.hasMoreElements()) { ZipEntry entry = (ZipEntry) entries.nextElement(); if (entry.isDirectory()) { // Create directory File dir = new File(output, entry.getName()); if (!dir.exists()) { dir.mkdirs(); } } else { // Get zipEntry name and create an output stream. File file = new File(output, entry.getName()); FileOutputStream fileOutputStream = new FileOutputStream(file); InputStream inputStream = zipFile.getInputStream(entry); // Read Zip file entry int i; byte[] data = new byte[1024]; while ((i = inputStream.read(data)) != -1) { fileOutputStream.write(data, 0, i); } fileOutputStream.close(); inputStream.close(); } } } catch (IOException e) { e.printStackTrace(); } finally { try { if (zipFile != null) { zipFile.close(); } } catch (IOException e) { e.printStackTrace(); } } } }
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/264093.html