整理自: The Java Developers Almanac 1.4
Compressing a File in the GZIP Format:
try
?{
????
//
?Create?the?GZIP?output?stream
????String?outFilename?
=
?
"
outfile.gzip
"
;
????GZIPOutputStream?out?
=
?
new
?GZIPOutputStream(
new
?FileOutputStream(outFilename));
????
//
?Open?the?input?file
????String?inFilename?
=
?
"
infilename
"
;
????FileInputStream?in?
=
?
new
?FileInputStream(inFilename);
????
//
?Transfer?bytes?from?the?input?file?to?the?GZIP?output?stream
????
byte
[]?buf?
=
?
new
?
byte
[
1024
];
????
int
?len;
????
while
?((len?
=
?in.read(buf))?
>
?
0
)?{
????????out.write(buf,?
0
,?len);
????}
????in.close();
????
//
?Complete?the?GZIP?file
????out.finish();
????out.close();
}?
catch
?(IOException?e)?{
}
Uncompressing a File in the GZIP Format:
try
?{
????
//
?Open?the?compressed?file
????String?inFilename?
=
?
"
infile.gzip
"
;
????GZIPInputStream?in?
=
?
new
?GZIPInputStream(
new
?FileInputStream(inFilename));
????
//
?Open?the?output?file
????String?outFilename?
=
?
"
outfile
"
;
????OutputStream?out?
=
?
new
?FileOutputStream(outFilename);
????
//
?Transfer?bytes?from?the?compressed?file?to?the?output?file
????
byte
[]?buf?
=
?
new
?
byte
[
1024
];
????
int
?len;
????
while
?((len?
=
?in.read(buf))?
>
?
0
)?{
????????out.write(buf,?
0
,?len);
????}
????
//
?Close?the?file?and?stream
????in.close();
????out.close();
}?
catch
?(IOException?e)?{
}
Creating a ZIP File:
//
?These?are?the?files?to?include?in?the?ZIP?file
String[]?filenames?
=
?
new
?String[]{
"
filename1
"
,?
"
filename2
"
};
//
?Create?a?buffer?for?reading?the?files
byte
[]?buf?
=
?
new
?
byte
[
1024
];
try
?{
????
//
?Create?the?ZIP?file
????String?outFilename?
=
?
"
outfile.zip
"
;
????ZipOutputStream?out?
=
?
new
?ZipOutputStream(
new
?FileOutputStream(outFilename));
????
//
?Compress?the?files
????
for
?(
int
?i
=
0
;?i
<
filenames.length;?i
++
)?{
????????FileInputStream?in?
=
?
new
?FileInputStream(filenames[i]);
????????
//
?Add?ZIP?entry?to?output?stream.
????????out.putNextEntry(
new
?ZipEntry(filenames[i]));
????????
//
?Transfer?bytes?from?the?file?to?the?ZIP?file
????????
int
?len;
????????
while
?((len?
=
?in.read(buf))?
>
?
0
)?{
????????????out.write(buf,?
0
,?len);
????????}
????????
//
?Complete?the?entry
????????out.closeEntry();
????????in.close();
????}
????
//
?Complete?the?ZIP?file
????out.close();
}?
catch
?(IOException?e)?{
}
Listing the Contents of a ZIP File:
try
?{
????
//
?Open?the?ZIP?file
????ZipFile?zf?
=
?
new
?ZipFile(
"
filename.zip
"
);
????
//
?Enumerate?each?entry
????
for
?(Enumeration?entries?
=
?zf.entries();?entries.hasMoreElements();)?{
????????
//
?Get?the?entry?name
????????String?zipEntryName?
=
?((ZipEntry)entries.nextElement()).getName();
????}
}?
catch
?(IOException?e)?{
}
Retrieving a Compressed File from a ZIP File:
try
?{
????
//
?Open?the?ZIP?file
????String?inFilename?
=
?
"
infile.zip
"
;
????ZipInputStream?in?
=
?
new
?ZipInputStream(
new
?FileInputStream(inFilename));
????
//
?Get?the?first?entry
????ZipEntry?entry?
=
?in.getNextEntry();
????
//
?Open?the?output?file
????String?outFilename?
=
?
"
o
"
;
????OutputStream?out?
=
?
new
?FileOutputStream(outFilename);
????
//
?Transfer?bytes?from?the?ZIP?file?to?the?output?file
????
byte
[]?buf?
=
?
new
?
byte
[
1024
];
????
int
?len;
????
while
?((len?
=
?in.read(buf))?
>
?
0
)?{
????????out.write(buf,?
0
,?len);
????}
????
//
?Close?the?streams
????out.close();
????in.close();
}?
catch
?(IOException?e)?{
}