原文链接:http://www.baeldung.com/java-compress-and-uncompress
1. Overview
In this quick tutorial, we’ll discuss how to zip a file into an archive and how to unzip the archive – all using core libraries provided by Java.
These core libraries are part of the java.util.zip package – where we can find all zipping and unzipping related utilities.
2. Zip a File
Let’s first have a look at a simple operation – zipping a single file.
For our example here we’ll zip a file named test1.txt into an archived named compressed.zip .
We’ll of course first access the file from disk – let’s have a look:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public
class
ZipFile {
public
static
void
main(String[] args)
throws
IOException {
String sourceFile =
"test1.txt"
;
FileOutputStream fos =
new
FileOutputStream(
"compressed.zip"
);
ZipOutputStream zipOut =
new
ZipOutputStream(fos);
File fileToZip =
new
File(sourceFile);
FileInputStream fis =
new
FileInputStream(fileToZip);
ZipEntry zipEntry =
new
ZipEntry(fileToZip.getName());
zipOut.putNextEntry(zipEntry);
final
byte
[] bytes =
new
byte
[
1024
];
int
length;
while
((length = fis.read(bytes)) >=
0
) {
zipOut.write(bytes,
0
, length);
}
zipOut.close();
fis.close();
fos.close();
}
}
3. Zip Multiple Files
Next, let’s see how to zip multiple files into one zip file. We will compress test1.txt and test2.txt into multiCompressed.zip :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public
class
ZipMultipleFiles {
public
static
void
main(String[] args)
throws
IOException {
List<String> srcFiles = Arrays.asList(
"test1.txt"
,
"test2.txt"
);
FileOutputStream fos =
new
FileOutputStream(
"multiCompressed.zip"
);
ZipOutputStream zipOut =
new
ZipOutputStream(fos);
for
(String srcFile : srcFiles) {
File fileToZip =
new
File(srcFile);
FileInputStream fis =
new
FileInputStream(fileToZip);
ZipEntry zipEntry =
new
ZipEntry(fileToZip.getName());
zipOut.putNextEntry(zipEntry);
byte
[] bytes =
new
byte
[
1024
];
int
length;
while
((length = fis.read(bytes)) >=
0
) {
zipOut.write(bytes,
0
, length);
}
fis.close();
}
zipOut.close();
fos.close();
}
}
4. Zip a Directory
Now, let’s discuss how to zip an entire directory. We will directory zipTest into dirCompressed.zip :
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
public
class
ZipDirectory {
public
static
void
main(String[] args)
throws
IOException {
String sourceFile =
"zipTest"
;
FileOutputStream fos =
new
FileOutputStream(
"dirCompressed.zip"
);
ZipOutputStream zipOut =
new
ZipOutputStream(fos);
File fileToZip =
new
File(sourceFile);
zipFile(fileToZip, fileToZip.getName(), zipOut);
zipOut.close();
fos.close();
}
private
static
void
zipFile(File fileToZip, String fileName, ZipOutputStream zipOut)
throws
IOException {
if
(fileToZip.isHidden()) {
return
;
}
if
(fileToZip.isDirectory()) {
File[] children = fileToZip.listFiles();
for
(File childFile : children) {
zipFile(childFile, fileName +
"/"
+ childFile.getName(), zipOut);
}
return
;
}
FileInputStream fis =
new
FileInputStream(fileToZip);
ZipEntry zipEntry =
new
ZipEntry(fileName);
zipOut.putNextEntry(zipEntry);
byte
[] bytes =
new
byte
[
1024
];
int
length;
while
((length = fis.read(bytes)) >=
0
) {
zipOut.write(bytes,
0
, length);
}
fis.close();
}
}
Note that:
To zip sub-directories, we iterate through them recursively. Every time we find a directory, we append its name to the descendants ZipEntry name to save the hierarchy.
5. Unzip an Archive
Let’s now unzip an archive and extract its contents.
For this example we’ll actually use the archive that got created in the previous code sample – compressed.zip – and we’ll unzip that into a new folder named unzipTest .
Let’s have a look:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public
class
UnzipFile {
public
static
void
main(String[] args)
throws
IOException {
String fileZip =
"compressed.zip"
;
byte
[] buffer =
new
byte
[
1024
];
ZipInputStream zis =
new
ZipInputStream(
new
FileInputStream(fileZip));
ZipEntry zipEntry = zis.getNextEntry();
while
(zipEntry !=
null
){
String fileName = zipEntry.getName();
File newFile =
new
File(
"unzipTest/"
+ fileName);
FileOutputStream fos =
new
FileOutputStream(newFile);
int
len;
while
((len = zis.read(buffer)) >
0
) {
fos.write(buffer,
0
, len);
}
fos.close();
zipEntry = zis.getNextEntry();
}
zis.closeEntry();
zis.close();
}
}
6. Conclusion
This tutorial illustrated how is simple using of Java libraries for operations of zipping and unzipping files.
The implementation of these examples can be found over on GitHub – this is a Maven-based project, so it should be easy to import and run as it is.