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

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包中

 

你可能感兴趣的:(java,jdk,log4j,ant,OS)