Java API中的 java.util.zip.*;包下包含了Java對於壓縮文件的所有相關操作。我們可以使用該包中的方法,結合IO中的相關知識,進行文件的壓縮和解壓縮相關操作。
ZipFile
java中的每一個壓縮文件都是可以使用ZipFile來進行表示的。
-
File file =
new File(
"F:/zippath.zip");
-
ZipFile zipFile =
new ZipFile(file);
-
System.out.println(
"壓縮文件的名稱為:" + zipFile.getName());
壓縮單個文件
-
/** 壓縮單個文件*/
-
public static void ZipFile(String filepath ,String zippath) {
-
try {
-
File file =
new File(filepath);
-
File zipFile =
new File(zippath);
-
InputStream input =
new FileInputStream(file);
-
ZipOutputStream zipOut =
new ZipOutputStream(
new FileOutputStream(zipFile));
-
zipOut.putNextEntry(
new ZipEntry(file.getName()));
-
int temp =
0;
-
while((temp = input.read()) != -
1){
-
zipOut.write(temp);
-
}
-
input.close();
-
zipOut.close();
-
}
catch (Exception e) {
-
e.printStackTrace();
-
}
-
}
應用:
-
/** 一次性壓縮多個文件,文件存放至一個文件夾中*/
-
public static void ZipMultiFile(String filepath ,String zippath) {
-
try {
-
File file =
new File(filepath);
// 要被壓縮的文件夾
-
File zipFile =
new File(zippath);
-
InputStream input =
null;
-
ZipOutputStream zipOut =
new ZipOutputStream(
new FileOutputStream(zipFile));
-
if(file.isDirectory()){
-
File[] files = file.listFiles();
-
for(
int i =
0; i < files.length; ++i){
-
input =
new FileInputStream(files[i]);
-
zipOut.putNextEntry(
new ZipEntry(file.getName() + File.separator + files[i].getName()));
-
int temp =
0;
-
while((temp = input.read()) != -
1){
-
zipOut.write(temp);
-
}
-
input.close();
-
}
-
}
-
zipOut.close();
-
}
catch (Exception e) {
-
e.printStackTrace();
-
}
-
}
應用:
-
/** 解壓縮(解壓縮單個文件)*/
-
public static void ZipContraFile(String zippath ,String outfilepath ,String filename) {
-
try {
-
File file =
new File(zippath);
//壓縮文件路徑和文件名
-
File outFile =
new File(outfilepath);
//解壓后路徑和文件名
-
ZipFile zipFile =
new ZipFile(file);
-
ZipEntry entry = zipFile.getEntry(filename);
//所解壓的文件名
-
InputStream input = zipFile.getInputStream(entry);
-
OutputStream output =
new FileOutputStream(outFile);
-
int temp =
0;
-
while((temp = input.read()) != -
1){
-
output.write(temp);
-
}
-
input.close();
-
output.close();
-
}
catch (Exception e) {
-
e.printStackTrace();
-
}
-
}
應用:
ZipInputStream類:
當我們需要解壓縮多個文件的時候,ZipEntry就無法使用了。
如果想操作更加復雜的壓縮文件,我們就必須使用ZipInputStream類。
-
/** 解壓縮(壓縮文件中包含多個文件)可代替上面的方法使用。
-
* ZipInputStream類
-
* 當我們需要解壓縮多個文件的時候,ZipEntry就無法使用了,
-
* 如果想操作更加復雜的壓縮文件,我們就必須使用ZipInputStream類
-
* */
-
public static void ZipContraMultiFile(String zippath ,String outzippath){
-
try {
-
File file =
new File(zippath);
-
File outFile =
null;
-
ZipFile zipFile =
new ZipFile(file);
-
ZipInputStream zipInput =
new ZipInputStream(
new FileInputStream(file));
-
ZipEntry entry =
null;
-
InputStream input =
null;
-
OutputStream output =
null;
-
while((entry = zipInput.getNextEntry()) !=
null){
-
System.out.println(
"解壓縮" + entry.getName() +
"文件");
-
outFile =
new File(outzippath + File.separator + entry.getName());
-
if(!outFile.getParentFile().exists()){
-
outFile.getParentFile().mkdir();
-
}
-
if(!outFile.exists()){
-
outFile.createNewFile();
-
}
-
input = zipFile.getInputStream(entry);
-
output =
new FileOutputStream(outFile);
-
int temp =
0;
-
while((temp = input.read()) != -
1){
-
output.write(temp);
-
}
-
input.close();
-
output.close();
-
}
-
}
catch (Exception e) {
-
e.printStackTrace();
-
}
-
}
應用:
-
ZipContraMultiFile(
"f:/zippath.zip",
"d:/");
-
ZipContraMultiFile(
"d:/hello.zip",
"d:/");