文件压缩与解压缩

public class ZipFileUtil {
	private static final String   ZIP_FORMAT   = "zip";
    private static final String   SYMBOL_POINT = ".";
    private static final String   SYMBOL_SLASH = "/";
    
    /**
     * 压缩文件
     * 
     * @param zipFileName 保存的压缩包文件路径
     * @param inputFile 需要压缩的文件夹或者文件路径
     * @return isSuccess
     */
    public static boolean zipFiles(String[] inputFiles,String zipFileName){
    	boolean isSuccess = false;
    	//文件名检测
    	 if (inputFiles == null || !isZipFile(zipFileName)) {
             return false;
         }
    	 ZipOutputStream zoStream = null;
         FileInputStream fiStream = null;
         FileOutputStream foStream = null;
         try {
             foStream = new FileOutputStream(zipFileName);
             zoStream = new ZipOutputStream(foStream);
             for (int i = 0; i < inputFiles.length; i++) {
                 fiStream = new FileInputStream(inputFiles[i]);
                 ZipEntry zipEntry = new ZipEntry(getFileName(inputFiles[i]));
                 zoStream.putNextEntry(zipEntry);
                 StreamUtil.io(fiStream, zoStream);
                 fiStream.close();
             }

             zoStream.closeEntry();
             isSuccess = true;
         } catch (FileNotFoundException e) {
             //log
             return false;
         } catch (IOException e) {
        	//log
             return false;
         } finally {
             if (fiStream != null) {
                 try {
                     fiStream.close();
                 } catch (IOException e) {
                	//log
                 }
             }

             if (zoStream != null) {
                 try {
                     zoStream.close();
                 } catch (IOException e) {
                	//log
                 }
             }
             if (foStream != null) {
                 try {
                     foStream.close();
                 } catch (IOException e) {
                	//log
                 }
             }
         }

    	 return isSuccess;
    }
    /**
     * 判断指定文件是否是zip文件
     * 
     * @param filePath
     * @return
     */
    private static boolean isZipFile(String filePath) {
        if (StringUtil.isBlank(filePath)) {
            return false;
        }

        String fileExtension = StringUtil.substringAfterLast(filePath, SYMBOL_POINT);
        return ZIP_FORMAT.equalsIgnoreCase(fileExtension);
    }
    /**
     * 截取文件名
     * 
     * @param pathName
     * @return
     */
    private static String getFileName(String filePath) {
        if (StringUtil.isBlank(filePath)) {
            return null;
        }

        boolean isFile = StringUtil.contains(filePath, SYMBOL_SLASH);
        if (isFile) {
            return StringUtil.substringAfterLast(filePath, SYMBOL_SLASH);
        } else {
            return filePath;
        }
    }
    
    /**
     * 解压ZIP文件 ZIP文件 --> files
     * 
     * @param destZipFile 压缩文件的完整路径名
     * @param unzipDir 解压后文件存储所在的目录
     * @return List<String> 存储了解压后文件路径名的列表,如果解压失败那么其size为0
     */
    public static List<String> unzipFiles(String unzipDir, String destZipFile) {
        List<String> resultFileList = new ArrayList<String>();

        if (!isZipFile(destZipFile)) {
            return resultFileList;
        }
        String unzipSubDir = getUnZipSubDir(unzipDir);
        try {
            FileInputStream in = new FileInputStream(destZipFile);
            unzipStreamToFiles(in, unzipSubDir, resultFileList);
        } catch (FileNotFoundException e) {
        	//log
            return resultFileList;
        }

        return resultFileList;
    }
    private static String getUnZipSubDir(String unzipDir) {
        File fileDir = new File(unzipDir);
        int fileNum = fileDir.list().length;
        int subDirNum = 0;
        String subDir = null;
        for(int i=0;i<fileNum;i++){
            if(fileDir.listFiles()[i].isDirectory()){
                subDirNum++;
            }
        }
       
        subDir = Integer.toString(subDirNum+1);
        if(subDirNum<10){
            subDir = "0"+subDir;
        }
        unzipDir = unzipDir+File.separator+subDir+File.separator;
        fileDir = new File(unzipDir);
        if (!fileDir.exists()) {
            new File(unzipDir).mkdir();
        }
        return unzipDir;
        
    }
    /**
     * 将zip输入流解压成文件,并将解压后的文件路径放在resultFileList中 注意如果传入的 resultFileList
     * 中有元素,那么原有的元素将被清除 如果指定的解压文件存储的目录不存在,那么本方法会自动建立该目录
     * 
     * @param zipedStream 待解压的zip输入流 (NOT NULL)
     * @param unzipDir 解压后的文件存储的路径 (NOT NULL)
     * @param resultFileList 保存解压完成文件路径名的文件名列表 (NOT NULL)
     * @return
     */
    public static boolean unzipStreamToFiles(InputStream zipedStream, String unzipDir,
                                             List<String> resultFileList) {
        if (zipedStream == null || StringUtil.isBlank(unzipDir) || resultFileList == null) {
            return false;
        }

        File dir = new File(unzipDir);
        if (!dir.exists()) {
            dir.mkdirs();
        }

        resultFileList.clear();

        ZipInputStream ziStream = null;
        FileOutputStream foStream = null;

        ziStream = new ZipInputStream(zipedStream);
        ZipEntry zipEntry;
        try {
            zipEntry = ziStream.getNextEntry();
            while (zipEntry != null) {
                String fileName = getFileName(zipEntry.getName());
                String destFile = unzipDir + fileName;
                foStream = new FileOutputStream(destFile);
                StreamUtil.io(ziStream, foStream);
                foStream.close();
                ziStream.closeEntry();
                zipEntry = ziStream.getNextEntry();
                resultFileList.add(destFile);
            }
        } catch (IOException e) {
        	//log
        } finally {
            if (foStream != null) {
                try {
                    foStream.close();
                } catch (IOException e) {
                	//log
                }
            }

            if (ziStream != null) {
                try {
                    ziStream.close();
                } catch (IOException e) {
                	//log
                }
            }
        }

        return true;
    }

}
 

你可能感兴趣的:(解压缩)