今天我们将看看Java zip文件的例子,我们还将使用Java程序压缩文件夹并创建Zip文件。
Java ZIP 应用程序
「java.util.zip.ZipOutputStream」可以用来将文件压缩为ZIP格式.由于一个Zip文件可以包含多个条目,因此ZipOutputStream使用「java.util.zip.ZipEntry」来表示一个Zip文件条目。
Java ZIP 檔案
Creating a zip archive for a single file is very easy, we need to create a ZipOutputStream object from the FileOutputStream object of destination file. Then we add a new ZipEntry to the ZipOutputStream and use FileInputStream to read the source file to ZipOutputStream object. Once we are done writing, we need to close ZipEntry and release all the resources.
Java Zip 文件夹
Zipping一个目录有点麻烦,首先我们需要把文件列表作为绝对路径,然后单独处理它们。我们需要为每个文件添加一个ZipEntry,并使用FileInputStream读取源文件的内容到相应的ZipEntry文件。
Java Zip 示例
以下是 java 程序,展示如何在 java 中 zip 一个单个文件或一个文件夹。
1package com.journaldev.files;
2
3import java.io.File;
4import java.io.FileInputStream;
5import java.io.FileOutputStream;
6import java.io.IOException;
7import java.util.ArrayList;
8import java.util.List;
9import java.util.zip.ZipEntry;
10import java.util.zip.ZipOutputStream;
11
12public class ZipFiles {
13
14 List<String> filesListInDir = new ArrayList<String>();
15
16 public static void main(String[] args) {
17 File file = new File("/Users/pankaj/sitemap.xml");
18 String zipFileName = "/Users/pankaj/sitemap.zip";
19
20 File dir = new File("/Users/pankaj/tmp");
21 String zipDirName = "/Users/pankaj/tmp.zip";
22
23 zipSingleFile(file, zipFileName);
24
25 ZipFiles zipFiles = new ZipFiles();
26 zipFiles.zipDirectory(dir, zipDirName);
27 }
28
29 /**
30 * This method zips the directory
31 * @param dir
32 * @param zipDirName
33 */
34 private void zipDirectory(File dir, String zipDirName) {
35 try {
36 populateFilesList(dir);
37 //now zip files one by one
38 //create ZipOutputStream to write to the zip file
39 FileOutputStream fos = new FileOutputStream(zipDirName);
40 ZipOutputStream zos = new ZipOutputStream(fos);
41 for(String filePath : filesListInDir){
42 System.out.println("Zipping "+filePath);
43 //for ZipEntry we need to keep only relative file path, so we used substring on absolute path
44 ZipEntry ze = new ZipEntry(filePath.substring(dir.getAbsolutePath().length()+1, filePath.length()));
45 zos.putNextEntry(ze);
46 //read the file and write to ZipOutputStream
47 FileInputStream fis = new FileInputStream(filePath);
48 byte[] buffer = new byte[1024];
49 int len;
50 while ((len = fis.read(buffer)) > 0) {
51 zos.write(buffer, 0, len);
52 }
53 zos.closeEntry();
54 fis.close();
55 }
56 zos.close();
57 fos.close();
58 } catch (IOException e) {
59 e.printStackTrace();
60 }
61 }
62
63 /**
64 * This method populates all the files in a directory to a List
65 * @param dir
66 * @throws IOException
67 */
68 private void populateFilesList(File dir) throws IOException {
69 File[] files = dir.listFiles();
70 for(File file : files){
71 if(file.isFile()) filesListInDir.add(file.getAbsolutePath());
72 else populateFilesList(file);
73 }
74 }
75
76 /**
77 * This method compresses the single file to zip format
78 * @param file
79 * @param zipFileName
80 */
81 private static void zipSingleFile(File file, String zipFileName) {
82 try {
83 //create ZipOutputStream to write to the zip file
84 FileOutputStream fos = new FileOutputStream(zipFileName);
85 ZipOutputStream zos = new ZipOutputStream(fos);
86 //add a new Zip Entry to the ZipOutputStream
87 ZipEntry ze = new ZipEntry(file.getName());
88 zos.putNextEntry(ze);
89 //read the file and write to ZipOutputStream
90 FileInputStream fis = new FileInputStream(file);
91 byte[] buffer = new byte[1024];
92 int len;
93 while ((len = fis.read(buffer)) > 0) {
94 zos.write(buffer, 0, len);
95 }
96
97 //Close the zip entry to write to zip file
98 zos.closeEntry();
99 //Close resources
100 zos.close();
101 fis.close();
102 fos.close();
103 System.out.println(file.getCanonicalPath()+" is zipped to "+zipFileName);
104
105 } catch (IOException e) {
106 e.printStackTrace();
107 }
108
109 }
110
111}
上面的Java Zip示例程序的输出是:
1/Users/pankaj/sitemap.xml is zipped to /Users/pankaj/sitemap.zip
2Zipping /Users/pankaj/tmp/.DS_Store
3Zipping /Users/pankaj/tmp/data/data.dat
4Zipping /Users/pankaj/tmp/data/data.xml
5Zipping /Users/pankaj/tmp/data/xmls/project.xml
6Zipping /Users/pankaj/tmp/data/xmls/web.xml
7Zipping /Users/pankaj/tmp/data.Xml
8Zipping /Users/pankaj/tmp/DB.xml
9Zipping /Users/pankaj/tmp/item.XML
10Zipping /Users/pankaj/tmp/item.xsd
11Zipping /Users/pankaj/tmp/ms/data.txt
12Zipping /Users/pankaj/tmp/ms/project.doc
请注意,当日志文件在目录中为 zip 时,我会打印绝对路径,但当我添加 zip 输入时,我会从目录中使用相对路径,以便当我们解码时,它会创建相同的目录结构。