java zip工具类


package com.dmx.recmanager.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
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.Enumeration;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;


public class ZipUtils {
static final int BUFFER = 1024;

public static void main(String[] args) {
try {
unZip("E:\\epg.zip", "E:\\epg_Temp\\");
//readEpgFile("D:\\epg");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public static void zip(String sourceDir, String zipFile) {
OutputStream os;
try {
os = new FileOutputStream(zipFile);
BufferedOutputStream bos = new BufferedOutputStream(os);
ZipOutputStream zos = new ZipOutputStream(bos);

File file = new File(sourceDir);

String basePath = null;
if (file.isDirectory()) {
basePath = file.getPath();
} else {
basePath = file.getParent();
}

zipFile(file, basePath, zos);

zos.closeEntry();
zos.close();
} catch (Exception e) {
e.printStackTrace();
}

}

/**
* @param source
* @param basePath
* @param zos
* @throws IOException
*/
private static void zipFile(File source, String basePath,
ZipOutputStream zos) {
File[] files = new File[0];

if (source.isDirectory()) {
files = source.listFiles();
} else {
files = new File[1];
files[0] = source;
}

String pathName;
byte[] buf = new byte[1024];
int length = 0;
try {
for (File file : files) {
if (file.isDirectory()) {
pathName = file.getPath().substring(basePath.length() + 1)+ "/";
zos.putNextEntry(new ZipEntry(pathName));
zipFile(file, basePath, zos);
} else {
pathName = file.getPath().substring(basePath.length() + 1);
InputStream is = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(is);
zos.putNextEntry(new ZipEntry(pathName));
while ((length = bis.read(buf)) > 0) {
zos.write(buf, 0, length);
}
is.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}

}

/**
* 解压缩zip文件
* @param fileName 要解压的文件名 包含路径 如:"c:\\test.zip"
* @param filePath 解压后存放文件的路径 如:"c:\\temp"
* @throws Exception
*/
public static void unZip(String fileName, String filePath) throws Exception{
ZipFile zipFile = new ZipFile(fileName,"GBK");
Enumeration emu = zipFile.getEntries();

while(emu.hasMoreElements()){
ZipEntry entry = (ZipEntry) emu.nextElement();
if (entry.isDirectory()){
new File(filePath+entry.getName()).mkdirs();
continue;
}
BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry));

File file = new File(filePath + entry.getName());
File parent = file.getParentFile();
if(parent != null && (!parent.exists())){
parent.mkdirs();
}
FileOutputStream fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos,BUFFER);

byte [] buf = new byte[BUFFER];
int len = 0;
while((len=bis.read(buf,0,BUFFER))!=-1){
fos.write(buf,0,len);
}
bos.flush();
bos.close();
bis.close();
}
zipFile.close();
}
/**
* batch read EpgFile
* @param fileUrl
*/
public static File[] readEpgFile(String fileUrl){

File epgfile = new File(fileUrl);
File[] fileArry = epgfile.listFiles();
return fileArry;
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值