java.util.zip包在解压缩文件方面入门使用
工作空闲之余,无意间点了一下java.util.zip包下面的一些源码看了一看。知道其使用的范围就是针对我们日常中关于
文件的压缩和解压的操作。写了一些常用的压缩文件(单个文件和多个文件的压缩)、解压文件(解压单个文件和文件夹)的
小小Demo。
package com.clark.zip;
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;
/**
* 单个文件压缩
* @author caolipeng
* @date 2014年12月22日 下午2:48:15
* @SingleFileZip.java
* @version 1.0
*/
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();
}
}
}
}