首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

ZIP 压缩/解压资料

2012-10-24 
ZIP 压缩/解压文件import org.apache.log4j.Loggerimport org.apache.tools.zip.ZipFileimport org.apac

ZIP 压缩/解压文件
import org.apache.log4j.Logger;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;
import org.apache.tools.zip.ZipEntry;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;

public class ZIPHelper {

private static Logger logger = Logger.getLogger(ZIPHelper.class);

public static final String ENCODING_GBK = "GBK";
public static final String ENCODING_UTF8 = "UTF-8";
public static final String ENCODING_ISO = "ISO-8859-1";

public static void zipped(String filePath)throws IOException {
zipped(filePath, false);
}
public static void zipped(String filePath,boolean isDelSource)throws IOException {
zipped(filePath, null,isDelSource);
}
public static void zipped(String filePath,String regex, boolean isDelSource)throws IOException {
zipped(filePath, regex,null,isDelSource);
}
public static void zipped(String filePath,String regex, String zipPath,boolean isDelSource)throws IOException {
zipped(filePath, regex,zipPath,ENCODING_GBK,isDelSource);
}
public static void zipped(String filePath,String regex, String zipPath,
String encoding,boolean isDelSource)throws IOException {
logger.debug("zipped...." + filePath);
ZipOutputStream zos = null;
File file = new File(filePath);
try {
if(zipPath == null){
if(file.isDirectory()){
zipPath = file.getPath()+".zip";
}else{
zipPath = file.getParent()+"/"+file.getName()+".zip";
}
logger.debug("zipped...." + zipPath);
zos = new ZipOutputStream(new FileOutputStream(zipPath));
}
else{
zos = new ZipOutputStream(new FileOutputStream(zipPath));
}

zos.setEncoding(encoding);
zippedFile(file, zos, "",regex,isDelSource);

} catch (FileNotFoundException e) {
logger.error("zipped ..."+e.getMessage());
throw e;
} finally {
try {
if (zos != null){
zos.close();
}
} catch (IOException e) {
logger.error("zipped ..."+e.getMessage());
throw e;
}
}
logger.debug("zipped ok....");
}

private static void zippedFile(File f, ZipOutputStream zos,
String hiberarchy,String regex,boolean isDelSource) throws IOException {
logger.debug("zipped...."+f.getName());
if (f.exists()) {
if (f.isDirectory()) {
hiberarchy += f.getName() + "/";
File[] fif = f.listFiles();
for (int i = 0; i < fif.length; i++) {
if(regex != null){
if(fif[i].getName().matches(regex)){
zippedFile(fif[i], zos, hiberarchy,regex,isDelSource);
}
}else{
zippedFile(fif[i], zos, hiberarchy,regex,isDelSource);
}
if(isDelSource){
if(!fif[i].delete()){
logger.debug("Delete files defeat..."+fif[i].getName());
}
}
}
} else {

FileInputStream fis = null;
try {
fis = new FileInputStream(f);
ZipEntry ze = new ZipEntry(hiberarchy + f.getName());
zos.putNextEntry(ze);
byte[] b = new byte[1024];
int slen;
while ((slen = fis.read(b, 0, b.length)) != -1)
zos.write(b, 0, slen);
} finally {
try {
if (fis != null)
fis.close();    
} catch (IOException e) {
logger.error(e.getMessage());
}
}
}
}
}





public static void unzip(String zipPath) throws IOException {
unzip(zipPath, false);
}
public static void unzip(String zipPath, boolean isDelZip)throws IOException {
unzip(zipPath, null,isDelZip);
}
public static void unzip(String zipPath, String extractPath, boolean isDelZip) throws IOException {
unzip(zipPath, extractPath, ENCODING_GBK,isDelZip);
}
public static void unzip(String zipPath, String extractPath,String encoding, boolean isDelZip) throws IOException {

logger.debug("unzip...." + zipPath);
File infile = new File(zipPath);
ZipFile zip = new ZipFile(infile,encoding);
Enumeration<ZipEntry> enums = zip.getEntries();

File destFile = null;
if(extractPath == null){
extractPath = infile.getParent();
}
while(enums.hasMoreElements()){
ZipEntry entry = enums.nextElement();
logger.debug("extract...."+entry.getName());
destFile = new File(extractPath, entry.getName());
unzipFile(destFile,zip,entry);
}
zip.close();
if(isDelZip){
if(!infile.delete()){
logger.debug("Delete files defeat..."+infile.getName());
}
}
logger.debug("unzip ok....");
}

private static void unzipFile(File destFile,ZipFile zip,ZipEntry entry) throws IOException {
try{
if(entry.isDirectory()){
destFile.mkdirs();
}else{
File file = destFile.getParentFile();
if(file != null && !file.exists()){
file.mkdirs();
}
FileOutputStream output = new FileOutputStream(destFile);
byte[] c = new byte[1024];
int slen;
InputStream input = zip.getInputStream(entry);
while ((slen = input.read(c, 0, c.length)) != -1)
output.write(c, 0, slen);

input.close();
output.close();
}
} catch (FileNotFoundException e) {
logger.error("unzip ..."+e.getMessage());
throw e;
}
}
}

热点排行