六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 40|回复: 0

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

[复制链接]

升级  98%

13

主题

13

主题

13

主题

童生

Rank: 1

积分
49
 楼主| 发表于 2013-1-28 19:03:06 | 显示全部楼层 |阅读模式
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包中
 
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表