java对zip文件进行解压并取出文件的名称

本文介绍了两种使用Java进行ZIP文件解压的方法:一种是通过JDK自带的类实现,但存在中文乱码的问题;另一种是借助Apache Ant工具,支持中文且更加灵活,尽管效率较低。

1、先用JDK的类进行ZIP文件解压

    public List releaseWinZIPByList(String zipPath, String serverPath) {

        //zipPath为需要解压的zip文件
        //serverPath为解压后文件的存放路径
        List list = null;
        ZipInputStream in = null;
        FileOutputStream os = null;
        try {
            in = new ZipInputStream(new FileInputStream(zipPath));
            ZipEntry entry = null;
            while ((entry = in.getNextEntry()) != null) {
                String entryName = entry.getName();
                if (entry.isDirectory()) {
                    File file = new File(serverPath + File.separator + entryName);
                    file.mkdirs();
                } else {
                    os = new FileOutputStream(serverPath + File.separator + entryName);
                    int bytesRead = 0;
                    while ((bytesRead = in.read()) != -1) {
                        os.write(bytesRead);
                    }
                }// 未取值
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            Log4J.logWarn(e.getMessage());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            Log4J.logWarn(e.getMessage());
        }
        return list;
    }

    JDK的解压的字符集只支持ISO-8859-1,所以说在解压的时候出现中文的地方会出现异常,可以采取先把文件转换成UUID的编码名称,当然这是没有业务需求的情况下!

2、利用ant解压文件

   public static void releaseZipFileList(String zipPath, String serverPath)
            throws Exception {
        //zipPath为需要解压的zip文件
        //serverPath为解压后文件的存放路径
        ZipFile zipFile = new ZipFile(zipPath);
        Enumeration e = zipFile.getEntries();
        org.apache.tools.zip.ZipEntry zipEntry = null;
        while (e.hasMoreElements()) {
            zipEntry = (org.apache.tools.zip.ZipEntry) e.nextElement();
            String entryName = zipEntry.getName();
            String fileNames[] = entryName.split("/");//读取文件集合
            int length = fileNames.length;
            String path = serverPath;
            for (int it = 0; it < length; it++) {
                if (it < length - 1) {
                    path += fileNames[it] + "/";
                    new File(path).mkdir();
                } else { // 读取最后的一个文件
                    if (entryName.endsWith("/")) { // 为目录,则创建文件夹
                        new File(serverPath + entryName).mkdir();
                    } else {
                        InputStream in = zipFile.getInputStream(zipEntry);
                        OutputStream os = new FileOutputStream(new File(
                                serverPath + entryName));
                        byte[] buf = new byte[1024];
                        int len;
                        while ((len = in.read(buf)) > 0) {
                            os.write(buf, 0, len);
                        }
                        in.close();
                        os.close();
                    }
                }
            }
        }
        zipFile.close();
    }

利用ant挤压可以支持中文,但是效率不高,有需要的时候可以用,用到的类在ant包中

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值