带密码解压zip文件和不带密码解压zip文件

第一篇写完下载ZIP文件,这次就是java实现解压ZIP

不带密码解压ZIP文件代码如下



import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import static java.awt.Window.log;

/**
* zip解压示例
*
*
*/
@Slf4j
public class ZipUtil {

   public static void main(String[] args) throws IOException {
       String filename = "F:\\administrator\\Desktop\\corp.zip";
       String path = "F:\\\\administrator\\\\Desktop\\\\jieya";
       ZipUtil.unZip(filename, path);
   }


   public static void unZip(String sourceFilename, String targetDir) throws IOException {
       unZip(new File(sourceFilename), targetDir);
   }

   /**
    * 将sourceFile解压到targetDir
    * @param sourceFile
    * @param targetDir
    * @throws RuntimeException
    */
   public static void unZip(File sourceFile, String targetDir) throws IOException {
       long start = System.currentTimeMillis();
       if (!sourceFile.exists()) {
           throw new FileNotFoundException("cannot find the file = " + sourceFile.getPath());
       }
       ZipFile zipFile = null;
       try{
           zipFile = new ZipFile(sourceFile);
           Enumeration<?> entries = zipFile.entries();
           while (entries.hasMoreElements()) {
               ZipEntry entry = (ZipEntry) entries.nextElement();
               if (entry.isDirectory()) {
                   String dirPath = targetDir + "/" + entry.getName();
                   createDirIfNotExist(dirPath);
               } else {
                   File targetFile = new File(targetDir + "/" + entry.getName());
                   createFileIfNotExist(targetFile);
                   InputStream is = null;
                   FileOutputStream fos = null;
                   try {
                       is = zipFile.getInputStream(entry);
                       fos = new FileOutputStream(targetFile);
                       int len;
                       byte[] buf = new byte[1024];
                       while ((len = is.read(buf)) != -1) {
                           fos.write(buf, 0, len);
                       }
                   }finally {
                       try{
                           fos.close();
                       }catch (Exception e){
                           log.warn("close FileOutputStream exception", e);
                       }
                       try{
                           is.close();
                       }catch (Exception e){
                           log.warn("close InputStream exception", e);
                       }
                   }
               }
           }
           log.info("解压完成,耗时:" + (System.currentTimeMillis() - start) +" ms");
       } finally {
           if(zipFile != null){
               try {
                   zipFile.close();
               } catch (IOException e) {
                   log.warn("close zipFile exception", e);
               }
           }
       }
   }

   public static void createDirIfNotExist(String path){
       File file = new File(path);
       createDirIfNotExist(file);
   }

   public static void createDirIfNotExist(File file){
       if(!file.exists()){
           file.mkdirs();
       }
   }

   public static void createFileIfNotExist(File file) throws IOException {
       createParentDirIfNotExist(file);
       file.createNewFile();
   }

//    public static void createParentDirIfNotExist(String filename){
//        File file = new File(filename);
//        createParentDirIfNotExist(file);
//    }

   public static void createParentDirIfNotExist(File file){
       createDirIfNotExist(file.getParentFile());
   }
}


带密码隔离解压zip文件

package com.sinosoft.Interfaces.utils;

import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.FileHeader;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 解压zip文件
 */
public class UnZipUtils {

    public void unZip(String source,String dest,String password){
        try {
            File zipFile = new File(source);
            ZipFile zFile = new ZipFile(zipFile); // 首先创建ZipFile指向磁盘上的.zip文件
            zFile.setFileNameCharset("UTF-8");
            File destDir = new File(dest); // 解压目录
            if (!destDir.exists()) {// 目标目录不存在时,创建该文件夹
                destDir.mkdirs();
           }
            if (zFile.isEncrypted()) {
                zFile.setPassword(password.toCharArray()); // 设置密码
            }
            zFile.extractAll(dest); // 将文件抽出到解压目录(解压)
            List<net.lingala.zip4j.model.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);
            for (File f : extractedFileList) {
                System.out.println(f.getAbsolutePath() + "文件解压成功!");
            }
        } catch (ZipException e) {
            e.printStackTrace();
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Delphi提供了一个 `TZipFile` 类来处理zip文件,但是它默认不支持解压密码zip文件。不过,我们可以使用第三方库 `zlib` 来实现解压密码zip文件。下面是一个示例代码: ```delphi uses System.ZLib, System.Zip; procedure UnzipFileWithPassword(const ZipFileName, DestDir, Password: string); var ZipFile: TFileStream; ZLibStream: TZDecompressionStream; Archive: TZipFile; Entry: TZipHeader; Buffer: array[0..1023] of Byte; BytesRead: Integer; DestFileName: string; begin ZipFile := TFileStream.Create(ZipFileName, fmOpenRead or fmShareDenyNone); try ZLibStream := TZDecompressionStream.Create(ZipFile); try Archive := TZipFile.Create; try Archive.ReadZip64EndOfCentralDirectoryRecord(ZipFile); Archive.ReadZip64EndOfCentralDirectoryLocator(ZipFile); Archive.ReadZip64CentralDirectory(ZipFile); Archive.ReadCentralDirectory(ZipFile); for Entry in Archive do begin if Entry.IsEncrypted then begin Entry.Password := Password; end; DestFileName := TPath.Combine(DestDir, Entry.FileName); Archive.ExtractToStream(Entry, ZLibStream, TFileStream.Create(DestFileName, fmCreate)); end; finally Archive.Free; end; finally ZLibStream.Free; end; finally ZipFile.Free; end; end; ``` 这里我们使用 `TZDecompressionStream` 来解压密码zip文件,并且需要设置每个文件密码。注意,这种方式并不安全,因为密码明文存储在代码中。如果需要更安全的方式,可以考虑使用加密算法来存储密码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值