Java读取压缩文件内容,还有zip4j简单使用(加密、解密)

今天,接到个任务,要求实现一个从压缩包中读取文件

我就简单找了下,使用Java自带的工具类中的

/**
     * 读取一个zip文件的内容,内容限定于字符文件
     * @param srcPath
     */
    public static void readFromZip(String srcPath) {
        ZipEntry zipEntry = null;
        ZipInputStream zipInputStream = null;
        InputStream in = null;
        try {
            ZipFile zipFile = new ZipFile(srcPath);
            in = new BufferedInputStream(new FileInputStream(srcPath));
            zipInputStream = new ZipInputStream(in);
            while ( ( zipEntry = zipInputStream.getNextEntry() ) != null) {
                if( !zipEntry.isDirectory() ) {
                    //这里得到压缩包中的一个文件
                    BufferedReader reader = null;
                    if ( zipEntry.getSize() > 0 ) {
                        reader = new BufferedReader(new InputStreamReader(zipFile.getInputStream(zipEntry)));
                        String line = null;
                        while ( ( line = reader.readLine() ) != null ) {
                            System.out.println(line);
                            //TODO 得到每一数据,进行处理
                        }
                        reader.close();
                    }
                }
            }
            zipInputStream.closeEntry();
        } catch (IOException e) {
            System.out.println("这里抛出异常1121");
            e.printStackTrace();
        } 
    }

不过又需要对已经加密的压缩文件进行处理,加密的文件进行处理,在网上找到Zip4j开源项目

 /**
     * 读取一个带密码的压缩文件的文件内容
     * @param zipFile 压缩文件
     * @param password 密码
     * @throws ZipException
     */
    public static void readFromZipWithPassword(String zipFile, String password) throws ZipException {
        long startTime = System.currentTimeMillis();
        ZipFile zipFile2 = new ZipFile(zipFile);
        //设置编码格式
        zipFile2.setFileNameCharset("GBK");
        if (!zipFile2.isValidZipFile()) {
            throw new ZipException("文件不合法或不存在");
        }
        //检查是否需要密码
        if (zipFile2.isEncrypted()) {
            zipFile2.setPassword(password);
        }
        List<FileHeader> fileHeaderList = zipFile2.getFileHeaders();
        for (int i = 0; i < fileHeaderList.size(); i++) {
            FileHeader fileHeader = fileHeaderList.get(i);
            BufferedReader reader = new BufferedReader(new InputStreamReader(zipFile2.getInputStream(fileHeader)));
            String line;
            try {
                while ( ( line = reader.readLine() ) != null) {
                    System.out.println(line);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.out.println("解压成功!");
        long endTime = System.currentTimeMillis();
        System.out.println("耗时:" + (endTime - startTime) + "ms");
    }

                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值