直接上代码:
package com.cy.store.util;
import java.io.*;
import java.util.zip.*;
public class Jieysuo {
//复制输入流的内容到输出流
public static void copy(InputStream in,OutputStream out)throws IOException{
byte[] buf = new byte[4096];
int bytesRead = 0;
while ((bytesRead = in.read(buf))!=-1){
out.write(buf,0,bytesRead);
}
}
// 压缩一个文件
public static void gzip (String fileName) throws IOException {
InputStream in = null;
String gzipFileName = fileName + ".gz";
OutputStream out = null;
try {
in = new BufferedInputStream(new FileInputStream(fileName));
out = new GZIPOutputStream(new BufferedOutputStream(new FileOutputStream(gzipFileName)));
copy(in,out);
}finally {
if(out != null){
out.close();
}
if(in != null){
in.close();
}
}
}
// 解压缩一个文件
public static void gunzip (String gzipFileName,String unzipFileName) throws IOException {
InputStream in = null;
OutputStream out = null;
try {
in = new GZIPInputStream(new BufferedInputStream(new FileInputStream(gzipFileName)));
out = new BufferedOutputStream(new FileOutputStream(unzipFileName));
copy(in,out);
}finally {
if(out != null){
out.close();
}
if(in != null){
in.close();
}
}
}
// 多个文件或文件夹压缩
public static void zip(File inFile,File zipFile) throws IOException{
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile)));
try {
if(!inFile.exists()){
throw new FileNotFoundException(inFile.getAbsolutePath());
}
inFile = inFile.getCanonicalFile();
String rootPath = inFile.getParent();
if(!rootPath.endsWith(File.separator)){
rootPath += File.separator;
}
addFileToZipOut(inFile,out,rootPath);
}finally {
out.close();
}
}
private static void addFileToZipOut(File file, ZipOutputStream out, String rootPath) throws IOException {
String relativePath = file.getCanonicalPath().substring(rootPath.length());
if(file.isFile()){
out.putNextEntry(new ZipEntry(relativePath));
InputStream in = new BufferedInputStream(new FileInputStream(file));
try {
copy(in,out);
}finally {
in.close();
}
}else {
out.putNextEntry(new ZipEntry(relativePath + File.separator));
for(File f: file.listFiles()){
addFileToZipOut(f,out,rootPath);
}
}
}
//解压文件
public static void unzip(File zipFile,String destDir)throws IOException{
ZipInputStream zin = new ZipInputStream(new BufferedInputStream(new FileInputStream(zipFile)));
if(!destDir.endsWith(File.separator)){
destDir += File.separator;
}
try {
ZipEntry entry = zin.getNextEntry();
while (entry != null){
extractZipEntry(entry,zin,destDir);
entry = zin.getNextEntry();
}
}finally {
zin.close();
}
}
private static void extractZipEntry(ZipEntry entry, ZipInputStream zin, String destDir) throws IOException {
if(!entry.isDirectory()){
File parent = new File(destDir + entry.getName()).getParentFile();
if(!parent.exists()){
parent.mkdirs();
}
OutputStream entryout = new BufferedOutputStream(new FileOutputStream(destDir + entry.getName()));
try {
copy(zin,entryout);
}finally {
entryout.close();
}
}else {
new File(destDir + entry.getName()).mkdirs();
}
}
}