Java如何以zip格式压缩文件

java.util.zip提供了用于读取和编写zip和gzip文件格式的类。

在这篇文章中,我们将学习如何以zip格式压缩文件和目录。 ZipOutputStream类用于将zip格式的数据写入输出流。 zip文件包含多个由ZipEntry类表示的条目。

压缩文件

以下是压缩文件的示例。文件:ZipFileExample.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.ZipOutputStream;  /**  * @author yiibai  */ public class ZipFileExample {   public static void main(String[] args) {     // Output zip file     File zipfile = new File("MyFile.zip");     // Input file to be compressed     File inFile = new File("file.txt");      FileInputStream fileInputStream = null;     FileOutputStream fileOutputStream = null;     ZipOutputStream zipOutputStream = null;     try {       fileInputStream = new FileInputStream(inFile);       fileOutputStream = new FileOutputStream(zipfile);       zipOutputStream = new ZipOutputStream(fileOutputStream);        // Create ZipEntry       ZipEntry entry = new ZipEntry(inFile.getName());        // Set position of stream to the start of entry data       zipOutputStream.putNextEntry(entry);        // Write data to zipOutputStream       byte[] data = new byte[1024];       int i;       while ((i = fileInputStream.read(data)) != -1) {         zipOutputStream.write(data, 0, i);       }        // Closes the current ZIP entry and positions the stream for writing       // the next entry       zipOutputStream.closeEntry();      } catch (IOException e) {       e.printStackTrace();     } finally {       try {         if (fileInputStream != null) {           fileInputStream.close();         }         if (zipOutputStream != null) {           zipOutputStream.close();         }       } catch (IOException e) {         e.printStackTrace();       }     }   } } 

压缩目录中的所有文件和子目录

此示例以递归方式遍历目录中的所有文件,并使用ZipOutputStream将其写入zip文件。

文件:ZipDirectoryExample.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.ZipOutputStream;  /**  * @author yiibai  */ public class ZipDirectoryExample {   public static void main(String[] args) {     File zipFile = new File("MyZip.zip");     File directory = new File("D:/Work/java");     FileOutputStream fileOutputStream = null;     ZipOutputStream zipOutputStream = null;     try {       fileOutputStream = new FileOutputStream(zipFile);       zipOutputStream = new ZipOutputStream(fileOutputStream);        // Create zip file       createZipFile(zipOutputStream, directory, directory.getAbsolutePath());      } catch (IOException e) {       e.printStackTrace();     } finally {       try {         if (zipOutputStream != null) {           zipOutputStream.close();         }       } catch (IOException e) {         e.printStackTrace();       }     }    }    /**    * @param zipOutputStream    * @param directory    * @throws IOException    */   private static void createZipFile(ZipOutputStream zipOutputStream, File directory, String zipEntryPath) throws IOException {     File[] files = directory.listFiles();     for (File file : files) {       if (file.isDirectory()) {         createZipFile(zipOutputStream, file, zipEntryPath);       } else {          FileInputStream fileInputStream = new FileInputStream(file);          // Create zipEntry         String filePath = file.getAbsolutePath();         ZipEntry entry = new ZipEntry(filePath.replace(zipEntryPath + File.separator, ""));          // Set position of stream to the start of entry data         zipOutputStream.putNextEntry(entry);          // Write data to zip output stream         byte data[] = new byte[1024];         int i;         while ((i = fileInputStream.read(data)) != -1) {           zipOutputStream.write(data, 0, i);         }          // Closes the current ZIP entry         zipOutputStream.closeEntry();          // Close file stream         fileInputStream.close();       }     }   } } 

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/264094.html

(0)
上一篇 2022年6月7日
下一篇 2022年6月7日

相关推荐

发表回复

登录后才能评论