本例以Windows, JDK1.7 为基础,来演示使用Java 压缩成ZIP格式的文件。

在API中有java.util.zip可以用来压缩ZIP格式的文件,主要使用FileOutputStream, ZipOutputStream, FileInputStream, ZipEntry 类.

流程:
首先FileInputStream读取文件,在ZipEntry添加文件名,最后使用ZipOutputStream写入文件内容。

压缩ZIP格式文件

CompressionFileInZip.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package com.devnp.zip;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class CompressionFileInZip {

public static void main(String[] args) {
// 将5.pdf 压缩成 test.zip
File inFile = new File("D:\\Person-Test\\temp\\5.pdf");

File outFile = new File("D:\\Person-Test\\temp\\test.zip");

compressionInZip(inFile, outFile);
}

public static void compressionInZip(File inFile, File outFile) {

FileOutputStream fos = null;
ZipOutputStream zos = null;
FileInputStream in = null;

try {
fos = new FileOutputStream(outFile);

zos = new ZipOutputStream(fos);

ZipEntry ze = new ZipEntry(inFile.getName());

zos.putNextEntry(ze);

in = new FileInputStream(inFile);

byte[] buffer = new byte[1024];
int len;
while ((len = in.read(buffer)) > 0) {
zos.write(buffer, 0, len);
}

if (ze != null)
zos.closeEntry();

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (in != null)
in.close();

if (zos != null)
zos.close();

if (fos != null)
fos.close();

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

System.out.println("Compression In Zip is Success.");

}

}
运行结果:
1
Compression In Zip is Success.

多文件压缩ZIP格式文件

读取"D:\Person-Test\\temp\" 所有下面的文件,压缩到"D:\Person-Test\test.zip"当中。

CompressionMultipleFileInZip.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package com.devnp.zip;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class CompressionMultipleFileInZip {

static List<File> fileList ;
static final String SOURCE_FOLDER = "D:\\Person-Test\\temp\\" ;

public static void main(String[] args) {
// TODO Auto-generated method stub

File inFile = new File(SOURCE_FOLDER);

fileList = new ArrayList<File>() ;

getAllFileList(inFile) ; //获取所有文件

File outFile = new File("D:\\Person-Test\\test.zip"); //压缩文件生成地址

compressionInZip(outFile, fileList);
}

/**
* 获取文件下面的所有文件
* @param file
*/
public static void getAllFileList(File file){
if(file.isFile())
fileList.add(file) ;
else {
File[] files = file.listFiles() ;

for (int i = 0; i < files.length; i++) {
getAllFileList(files[i]) ;
}
}

}

public static void compressionInZip(File outFile, List<File> inFile) {

FileOutputStream fos = null;
ZipOutputStream zos = null;
FileInputStream in = null;

try {
fos = new FileOutputStream(outFile);

zos = new ZipOutputStream(fos);

for (int i = 0; i < inFile.size(); i++) {

String zipName = getZipEntryName(inFile.get(i).getAbsolutePath()) ;

System.out.println("Zip File Name : " + zipName);

ZipEntry ze = new ZipEntry(zipName);

zos.putNextEntry(ze);

in = new FileInputStream(inFile.get(i));

byte[] buffer = new byte[1024];
int len;
while ((len = in.read(buffer)) > 0) {
zos.write(buffer, 0, len);
}

if (ze != null)
zos.closeEntry();
}

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (in != null)
in.close();

if (zos != null)
zos.close();

if (fos != null)
fos.close();

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

System.out.println("Compression In Zip is Success.");

}

/**
* 根据文件绝对路径,截取压缩文件名
* @param fileAbsolutePath
* @return
*/
public static String getZipEntryName(String fileAbsolutePath){

return fileAbsolutePath.substring(SOURCE_FOLDER.length());
}

}
运行结果:
1
2
3
4
5
6
Zip File Name : 1\1.txt
Zip File Name : 2\3\New Text Document.txt
Zip File Name : 2.txt
Zip File Name : 5.pdf
Zip File Name : sudent.txt
Compression In Zip is Success.

相关

关于解压ZIP格式的文件 JAVA FILE DECOMPRESS 解压ZIP格式的文件

参考

ZipEntry