java Apache zip 支持中文 解压

本文介绍了一个使用 Apache 工具包实现的 Java 类 ApacheZipUtils,该类提供了支持中文的文件压缩与解压缩功能。通过递归方式处理文件夹及其子文件,确保了中文文件名的正确处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipInputStream;

public class ApacheZipUtils{
/*
 * 能支持中文的压缩
 * 参数base 开始为""
 * first 开始为true
 */
    public static void zip(org.apache.tools.zip.ZipOutputStream out, File f, String base,boolean first) throws Exception {
        if(first){
         if(f.isDirectory()){
        out.putNextEntry(new org.apache.tools.zip.ZipEntry("/"));
        base = base +f.getName();
        first = false;
         }
         else
          base = f.getName();
        }
     if (f.isDirectory()) {
           File[] fl = f.listFiles();
           base = base +"/";
           for (int i = 0; i < fl.length; i++) {
           zip(out, fl[i], base + fl[i].getName(),first);
         }
        }else {
           out.putNextEntry(new org.apache.tools.zip.ZipEntry(base));
           FileInputStream in = new FileInputStream(f);
           int b;
           System.out.println(base);
           while ( (b = in.read()) != -1) {
            out.write(b);
         }
         in.close();
       }
    }
    
    public static final String bytesToHexString(byte[] bArray) {
        StringBuffer sb = new StringBuffer(bArray.length);
        String sTemp;
        for (int i = 0; i < bArray.length; i++) {
         sTemp = Integer.toHexString(0xFF & bArray[i]);
         if (sTemp.length() < 2)
          sb.append(0);
          sb.append(sTemp.toUpperCase());
          sb.append(", ");
        }
        return sb.toString();
    }

    
    
    /*
     * 解压文件
     * unZip为解压路径
     */
    public static void unZipFileByApache(org.apache.tools.zip.ZipFile zipFile,String unZipRoot) throws Exception, IOException{
      System.out.println("encoding "+zipFile.getEncoding());
      java.util.Enumeration e = zipFile.getEntries();
      org.apache.tools.zip.ZipEntry zipEntry;
      while(e.hasMoreElements()){
       zipEntry = (org.apache.tools.zip.ZipEntry)e.nextElement();
       InputStream fis = zipFile.getInputStream(zipEntry);
       if(zipEntry.isDirectory()){
       }
       else{
         String filePath = unZipRoot+File.separator+zipEntry.getName();
         File file = new File(filePath);              
         File parentFile = file.getParentFile();
         parentFile.mkdirs();
         FileOutputStream fos = new FileOutputStream(file);
         byte[] b = new byte[1024];
         int len;
         while((len=fis.read(b, 0, b.length))!=-1){
         fos.write(b, 0, len);
         }
         fos.close();
         fis.close();
       }
      }
    }
    
    public static void ZipFile(String zipFileName,String inputFileName) throws Exception{
     org.apache.tools.zip.ZipOutputStream out = new org.apache.tools.zip.ZipOutputStream(new FileOutputStream(zipFileName));
        File inputFile = new File(inputFileName);
     zip(out, inputFile, "",true);
        System.out.println("zip done");
        out.close();
    }
    
    public static void unZipFile(String unZipFileName,String unZipPath) throws Exception{
     org.apache.tools.zip.ZipFile zipFile = new org.apache.tools.zip.ZipFile(unZipFileName, "gbk");
     unZipFileByApache(zipFile, unZipPath);
     System.out.println("unZip Ok");
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值