enterys的gravatar头像
enterys 2014-11-10 11:34:31

java org.apache.tools.zip压缩多个文件工具类代码

导包的时候一定导org.apache.tools.zip下的包,不要导JDK的zip包,JDK的包在文件名为中文的情况下,打包是会乱码的。在apache的包下,可以设置编码。直接看代码:

import java.io.BufferedInputStream;  
import java.io.DataInputStream;  
import java.io.File;  
import java.io.FileInputStream;  
import java.io.FileNotFoundException;  
import java.io.FileOutputStream;  
import java.io.IOException;  
  
import org.apache.tools.zip.ZipEntry;  
import org.apache.tools.zip.ZipOutputStream;  
import org.slf4j.Logger;  
import org.slf4j.LoggerFactory;  
  
  
public class ZipUtils {  
    private static final Logger log = LoggerFactory.getLogger(ZipUtils.class);  
          
    private ZipUtils(){};  
   /** 
     * 创建ZIP文件 
     * @param sourcePath 文件或文件夹路径 
     * @param zipPath 生成的zip文件存在路径(包括文件名) 
     */  
    public static void createZip(String sourcePath, String zipPath) {  
        FileOutputStream fos = null;  
        ZipOutputStream zos = null;  
        try {  
            fos = new FileOutputStream(zipPath);  
            zos = new ZipOutputStream(fos);  
            writeZip(new File(sourcePath), "", zos);  
        } catch (FileNotFoundException e) {  
            log.error("创建ZIP文件失败",e);  
        } finally {  
            try {  
                if (zos != null) {  
                    zos.close();  
                }  
            } catch (IOException e) {  
                log.error("创建ZIP文件失败",e);  
            }  
  
        }  
    }  
      
    private static void writeZip(File file, String parentPath, ZipOutputStream zos) {  
        if(file.exists()){  
            //处理文件夹  
            if(file.isDirectory()){  
                parentPath+=file.getName()+File.separator;  
                File [] files=file.listFiles();  
                for(File f:files){  
                    writeZip(f, parentPath, zos);  
                }  
            }else{  
                FileInputStream fis=null;  
                DataInputStream dis=null;  
                try {  
                    fis=new FileInputStream(file);  
                    dis=new DataInputStream(new BufferedInputStream(fis));  
                    ZipEntry ze = new ZipEntry(parentPath + file.getName());  
                    zos.putNextEntry(ze);  
                    //添加编码,如果不添加,当文件以中文命名的情况下,会出现乱码  
                    // ZipOutputStream的包一定是apache的ant.jar包。JDK也提供了打压缩包,但是不能设置编码  
                    zos.setEncoding("GBK");  
                    byte [] content=new byte[1024];  
                    int len;  
                    while((len=fis.read(content))!=-1){  
                        zos.write(content,0,len);  
                        zos.flush();  
                    }  
                } catch (FileNotFoundException e) {  
                    log.error("创建ZIP文件失败",e);  
                } catch (IOException e) {  
                    log.error("创建ZIP文件失败",e);  
                }finally{  
                    try {  
                        if(dis!=null){  
                            dis.close();  
                        }  
                    }catch(IOException e){  
                        log.error("创建ZIP文件失败",e);  
                    }  
                }  
            }  
        }  
    }      
    public static void main(String[] args) {  
        //测试把F盘下的所有文件打包压缩成sql.zip文件放在F盘根目录下  
        ZipUtils.createZip("F:/", "F:/sql.zip");  
          
    }  
}  

打赏

最代码最近下载分享源代码列表最近下载
最代码最近浏览分享源代码列表最近浏览
tangjun  LV17 2022年8月31日
xuexizhuanyong23  LV16 2022年5月22日
wanglinddad  LV54 2022年4月22日
cc900118  LV17 2021年11月1日
626364708  LV4 2020年9月4日
aorilee  LV1 2020年6月16日
wei112233  LV15 2020年4月25日
285112298  LV1 2020年3月25日
13043860zj  LV16 2020年3月6日
G你太美  LV9 2019年12月30日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友