解压缩文件

package com.hdz.common;


import java.io.BufferedOutputStream;
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;


import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;


/**
 * @author huangdezhi
 * 2017/2/10
 * DES:压缩、解压缩
 * 需要使用ant.jar
 */
public class ZipUtil {
private String encoding = System.getProperty("file.encoding");
private static byte[]          buf=new byte[1024*512]; 
    private static int             readedBytes; 
  
public static void zip(String sourcefile,String targetFile){
ZipOutputStream zipOut=null;
String zipName="";
File sfile=new File(sourcefile);
if(isExistsSource(sfile)){//待压缩的目录或文件存在
zipName=getZipFileName(sfile,targetFile);
if(!zipName.equals("")){
try {
zipOut=new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipName)));
// 添加对应的文件Entry
addEntry("",sfile,zipOut);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
zipOut.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}else{
System.out.println("source file error");
}
}
/**
* 扫描添加文件Entry
* @param base   基路径
* @param source 源文件
* @param zos    Zip文件输出流
*/
private static void addEntry(String base, File source, ZipOutputStream zos){
String entry = base + source.getName();
if(source.isDirectory()){
for(File f:source.listFiles()){
//递归列出目录下的所有文件,添加文件Entry
addEntry(entry + "/", f, zos);
}
}else{
FileInputStream fileIn;

try {
fileIn = new FileInputStream(source);
zos.putNextEntry(new ZipEntry(entry)); 
//这个是系统文件编码,不加,中文文件名会出现乱码
zos.setEncoding(System.getProperty("sun.jnu.encoding"));
while((readedBytes = fileIn.read(buf))>0){ 
zos.write(buf , 0 , readedBytes); 
           }
zos.closeEntry();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 判断压缩源是否存在
* @param file 待压缩的文件或目录
* @return 存在返回true 否则返回false
*/
private static boolean isExistsSource(File file){
if(file.exists())
return true;
else
return false;
}
/**
* 返回zip文件的路径
* @param file       源文件
* @param targetFile 指定的zip文件路径
* @return  zip文件的路径
*/
private static String getZipFileName(File file,String targetFile){
String zipName="";
if(file.isFile())
zipName=file.getName().substring(0, file.getName().lastIndexOf("."))+".zip";
else
zipName=file.getName()+".zip";

if(targetFile!=null&&!targetFile.trim().equals("")){//指定了压缩文件的输出目录
File tf=new File(targetFile);
if(tf.exists()){
zipName=targetFile+"\\"+zipName;
}else{
zipName="";
System.out.println("error targetFile");
}
}else{
zipName=file.getParent()+"\\"+zipName;
}
return zipName;
}
//设置缓冲区大小 
    public static void setBufSize(int bufSize){
        buf = new byte[bufSize]; 
    } 
    /**
     * 解压缩文件
     * @param zipfile    zip文件路径
     * @param targetpath 目标的目录
     */
    public static void unzip(String zipfile,String targetpath){
    File source = new File(zipfile);
    if (source.exists()) {
    //压缩后的目录
    if(targetpath==null||targetpath.trim().equals(""))
    targetpath=source.getParent();
    File outFile=new File(targetpath);
    //
    try {
ZipFile zipFile = new ZipFile(source,System.getProperty("sun.jnu.encoding"));
Enumeration<ZipEntry> em = zipFile.getEntries();
while(em.hasMoreElements()){
ZipEntry ze = em.nextElement();
File outItemFile = new File(outFile,ze.getName());
if(ze.isDirectory()){
outItemFile.mkdirs();
}else{
InputStream is = zipFile.getInputStream(ze);
outItemFile.getParentFile().mkdirs();
FileOutputStream fos = new FileOutputStream(outItemFile);
while((readedBytes=is.read(buf))!=-1){    
fos.write(buf, 0, readedBytes);    

fos.close(); 
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
    }
    }
}



package test.hdz.common;


import org.junit.Test;


import com.hdz.common.ZipUtil;


/**
 * @author huangdezhi
 * 2017/2/10
 * DES:
 */
public class ZipUtilTest {


@Test
public void zip(){
ZipUtil.zip("D:\\test\\zip\\測試中文.txt", "");
}
@Test
public void unzip(){
ZipUtil.unzip("D:\\test\\zip\\測試中文.zip", "D:\\test\\zip\\unzip");
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值