import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
public class FileZip
{
public static void main(String[] args)
{
/*getZipFileName();*/
/**壓縮單個文件*/
/*String filepath = "F:/iBoxClearDB_Orcl.sql";
String zippath = "F:/test/db.zip";
ZipFile(filepath, zippath);*/
/**一次性壓縮多個文件,文件存放至一個文件夾中*/
/*String filepath = "E:/工作週報/";
String zippath = "F:/test/week-job.zip";
ZipMultiFile(filepath, zippath);*/
/** 解壓縮(解壓縮單個文件)*/
/*String zippath = "F:/test/db.zip";
String outfilepath = "F:/test.sql";
String filename = "iBoxClearDB_Orcl.sql";
zipContraFile(zippath, outfilepath, filename);*/
/**解壓縮(壓縮文件中包含多個文件)*/
String zippath = "F:/test/week-job.zip";
String outfilepath = "F:/";
ZipContraMultiFile(zippath, outfilepath);
}
/**得到zip文件的文件名*/
private static void getZipFileName()
{
File file = new File("F:/test/temp.zip");
ZipFile zipFile = null;
try
{
zipFile = new ZipFile(file);
System.out.println("zip file name is :" + zipFile.getName());
}
catch (ZipException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
if (zipFile != null)
{
try
{
zipFile.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
/** 壓縮單個文件*/
public static void zipFile(String filepath, String zippath)
{
InputStream input = null;
ZipOutputStream zipOut = null;
try
{
File file = new File(filepath);
File zipFile = new File(zippath);
input = new FileInputStream(file);
zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
zipOut.putNextEntry(new ZipEntry(file.getName()));
int temp = 0;
while ((temp = input.read()) != -1)
{
zipOut.write(temp);
}
System.out.println("zip " + filepath + " to " + zippath);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
input.close();
zipOut.close();
}
catch (IOException 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();
}
}
else
{//否則,則調用壓縮單個文件的方法
zipFile(filepath, zippath);
}
zipOut.close();
System.out.println("zip directory is success");
}
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類
* */
public static void ZipContraMultiFile(String zippath, String outzippath)
{
ZipInputStream zipInput = null;
ZipFile zipFile = null;
InputStream input = null;
OutputStream output = null;
try
{
File file = new File(zippath);
File outFile = null;
zipFile = new ZipFile(file);
zipInput = new ZipInputStream(new FileInputStream(file));
ZipEntry entry = 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);
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
input.close();
output.close();
zipInput.close();
zipFile.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
Java----文件的壓縮與解壓
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.