首页 > 其他 > 详细

带秘钥的文件解压缩

时间:2017-07-06 15:44:40      阅读:303      评论:0      收藏:0      [点我收藏+]

我这次运用的有秘钥的解密,用了ZIP4J;

pom文件中这样引用:

<dependency>
      <groupId>net.lingala.zip4j</groupId>
      <artifactId>zip4j</artifactId>
      <version>1.3.1</version>
</dependency>

java代码如下:

/**
     * 使用给定密码解压指定的ZIP压缩文件到指定目录
     * <p>
     * 如果指定目录不存在,可以自动创建,不合法的路径将导致异常被抛出
     * 
     * @param zip
     *            指定的ZIP压缩文件
     * @param dest
     *            解压目录
     * @param passwd
     *            ZIP文件的密码
     * @return 解压后文件数组
     * @throws ZipException
     *             压缩文件有损坏或者解压缩失败抛出
     */
    public static File[] unzip(File zipFile, String dest, String passwd) throws ZipException {
        ZipFile zFile = new ZipFile(zipFile);
        zFile.setFileNameCharset("UTF-8");
        if (!zFile.isValidZipFile()) {
            throw new ZipException("压缩文件不合法,可能被损坏.");
        }
        File destDir = new File(dest);
        if (destDir.isDirectory() && !destDir.exists()) {
            destDir.mkdir();
        }
        if (zFile.isEncrypted()) {
            zFile.setPassword(passwd.toCharArray());
        }
        zFile.extractAll(dest);

        List<FileHeader> headerList = zFile.getFileHeaders();
        List<File> extractedFileList = new ArrayList<File>();
        for (FileHeader fileHeader : headerList) {
            if (!fileHeader.isDirectory()) {
                extractedFileList.add(new File(destDir, fileHeader.getFileName()));
            }
        }
        File[] extractedFiles = new File[extractedFileList.size()];
        extractedFileList.toArray(extractedFiles);
        return extractedFiles;
    }

很方便,我参考的http://blog.csdn.net/silversupersoul/article/details/44221607这篇文章,详细的点此连接

带秘钥的文件解压缩

原文:http://www.cnblogs.com/bing521meng/p/7126526.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!