【转载】JAVA压缩 解压缩zip 并解决linux下中文乱码

再压缩前,要设置linux模式, 需要使用第三方ant-1.6.5.jar
如果是文件目录,则
ZipEntry zipEntry=new ZipEntry(basePath + System.getProperties().getProperty(“file.separator”));
zipEntry.setUnixMode(755);//解决linux乱码

如果是文件,则
ZipEntry zipEntry=new ZipEntry(base);
zipEntry.setUnixMode(644);//解决linux乱码

然后在输出时强制设置编码:
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
zipFileName));
out.setEncoding(“GBK”);//解决linux乱码

全部代码如下:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;

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

import org.apache.tools.ant.Project;  

import org.apache.tools.ant.taskdefs.Expand;

/**
 * 文件夹压缩,支持win和linux
 * @author wlzhang
 *
 */
public class ZipUtil
{
    /**
     * @param inputFileName
     *            输入一个文件夹
     * @param zipFileName
     *            输出一个压缩文件夹,打包后文件名字
     * @throws Exception
     */
    public static OutputStream zip(String inputFileName, String zipFileName) throws Exception
    {
        return zip(zipFileName, new File(inputFileName));
    }

    private static OutputStream zip(String zipFileName, File inputFile) throws Exception
    {
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
                zipFileName));
        out.setEncoding("UTF-8");//解决linux乱码  根据linux系统的实际编码设置,可以使用命令 vi/etc/sysconfig/i18n 查看linux的系统编码
        zip(out, inputFile, "");
        out.close();
        return out;
    }

    private static void zip(ZipOutputStream out, File f, String base) throws Exception
    {
        if (f.isDirectory())
        { // 判断是否为目录
            File[] fl = f.listFiles();
            // out.putNextEntry(new org.apache.tools.zip.ZipEntry(base + "/"));
//            out.putNextEntry(new ZipEntry(base + "/"));
            ZipEntry zipEntry=new ZipEntry(base + System.getProperties().getProperty("file.separator"));
            zipEntry.setUnixMode(755);//解决linux乱码
            out.putNextEntry(zipEntry);
//            base = base.length() == 0 ? "" : base + "/";
            base = base.length() == 0 ? "" : base + System.getProperties().getProperty("file.separator");
            for (int i = 0; i < fl.length; i++)
            {
                zip(out, fl[i], base + fl[i].getName());
            }
        }
        else
        { // 压缩目录中的所有文件
            // out.putNextEntry(new org.apache.tools.zip.ZipEntry(base));
            ZipEntry zipEntry=new ZipEntry(base);
            zipEntry.setUnixMode(644);//解决linux乱码
            out.putNextEntry(zipEntry);
            FileInputStream in = new FileInputStream(f);
            int b;
            while ((b = in.read()) != -1)
            {
                out.write(b);
            }
            in.close();
        }
    }

    private static void unzip(String sourceZip,String destDir) throws Exception{  

        try{  

            Project p = new Project();  

            Expand e = new Expand();  

            e.setProject(p);  

            e.setSrc(new File(sourceZip));  

            e.setOverwrite(false);  

            e.setDest(new File(destDir));  

            /* 

            ant下的zip工具默认压缩编码为UTF-8编码, 

            而winRAR软件压缩是用的windows默认的GBK或者GB2312编码 

            所以解压缩时要制定编码格式 

            */ 

            e.setEncoding("UTF-8");  //根据linux系统的实际编码设置

            e.execute();  

        }catch(Exception e){  

            throw e;  

        }  

    }  


}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中使用Zip压缩中文文件名可能会出现乱码的问题,这是因为Zip文件格式默认使用的是ASCII编码,而中文字符需要使用其他编码方式进行处理。解决这个问题的方法有两种: 1. 使用Apache Commons Compress库 可以使用Apache Commons Compress库来压缩文件,这个库支持多种编码方式,可以解决中文文件名乱码的问题。示例代码如下: ```java import org.apache.commons.compress.archivers.ArchiveEntry; import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; import org.apache.commons.compress.utils.IOUtils; import java.io.*; public class ZipUtil { public static void zip(String sourcePath, String destPath) throws IOException { FileOutputStream fos = new FileOutputStream(destPath); ZipArchiveOutputStream zos = new ZipArchiveOutputStream(fos); File file = new File(sourcePath); String basePath = file.getParent(); compress(file, zos, basePath); zos.close(); fos.close(); } private static void compress(File file, ZipArchiveOutputStream zos, String basePath) throws IOException { if (file.isDirectory()) { File[] files = file.listFiles(); if (files.length == 0) { String path = file.getPath().substring(basePath.length() + 1) + "/"; ArchiveEntry entry = zos.createArchiveEntry(new File(file.getPath()), path); zos.putArchiveEntry(entry); zos.closeArchiveEntry(); } else { for (File f : files) { compress(f, zos, basePath); } } } else { String path = file.getPath().substring(basePath.length() + 1); ArchiveEntry entry = zos.createArchiveEntry(new File(file.getPath()), path); zos.putArchiveEntry(entry); InputStream is = new FileInputStream(file); IOUtils.copy(is, zos); is.close(); zos.closeArchiveEntry(); } } } ``` 2. 使用JavaZipOutputStream类 Java中自带的ZipOutputStream类也可以用于压缩文件,但是需要手动设置编码方式,示例代码如下: ```java import java.io.*; import java.nio.charset.Charset; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class ZipUtil { private static final int BUFFER_SIZE = 2 * 1024; public static void zip(String sourcePath, String destPath) throws IOException { FileOutputStream fos = new FileOutputStream(destPath); ZipOutputStream zos = new ZipOutputStream(fos, Charset.forName("GBK")); File file = new File(sourcePath); String basePath = file.getParent(); compress(file, zos, basePath); zos.close(); fos.close(); } private static void compress(File file, ZipOutputStream zos, String basePath) throws IOException { if (file.isDirectory()) { File[] files = file.listFiles(); if (files.length == 0) { String path = file.getPath().substring(basePath.length() + 1) + "/"; ZipEntry entry = new ZipEntry(path); zos.putNextEntry(entry); zos.closeEntry(); } else { for (File f : files) { compress(f, zos, basePath); } } } else { String path = file.getPath().substring(basePath.length() + 1); ZipEntry entry = new ZipEntry(path); zos.putNextEntry(entry); InputStream is = new FileInputStream(file); byte[] buffer = new byte[BUFFER_SIZE]; int len; while ((len = is.read(buffer)) != -1) { zos.write(buffer, 0, len); } is.close(); zos.closeEntry(); } } } ``` 以上两种方法都可以解决中文文件名乱码的问题,具体使用哪一种方法可以根据实际需要选择。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值