首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用java提取ZIP

用java提取ZIP
EN

Stack Overflow用户
提问于 2016-09-24 07:31:26
回答 2查看 1.4K关注 0票数 0

各位,

我正在使用zip4j API提取java中的文件,并能够提取这些文件。

  1. 我曾经将完整的目录压缩成zip,它包含文件和嵌套目录,使用 zipFile.addFolder(fileDirectory,参数);//ZIP目录文件/文件夹
  2. 使用 ZipFile zipFile =新的ZipFile(stringArchievedFile);//将所有文件提取到指定的路径zipFile.extractAll(stringExtractingFilePath);

问题是在提取之后,应该将文件解压缩到zipFile.extractAll(path)方法提供的路径上,但是正在创建另一个目录。如何将文件解压缩到实际指定的目录中?

类似:提取路径 C:\ExtractionPath

文件路径 C:\SelectingPath\File1 1

C:\SelectingPath\File2 2

C:\SelectingPath\Directory1 1\File1 1

C:\SelectingPath\Directory2 2\File1 1

我将选择要压缩的C:\SelectingPath目录

我将选择C:\ExtractionPath目录来解压缩文件。

提取之后,所有提取的文件都将进入

**C:\ExtractionPath\SelectingPath**

我需要目录中的所有文件

**C:\ExtractionPath**本身。

请帮我解决这个问题。

提前感谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-09-26 05:05:17

谢谢安德烈亚斯!

我们需要将ArrayList<File>作为参数传递给addFiles(ArrayList, ZipParameters)方法,以便归档目录的整个目录内容。我得到了预期的输出

遵循归档代码流:

代码语言:javascript
运行
复制
public void archieveFiles(File fileDirectory, String stringPassword) throws Exception {
        try{

            String[] filesDirectoryList = fileDirectory.list();

            ArrayList<File> listFileDirectory = new ArrayList<>(); //To list the files to archive
            for(int iListCount = 0; iListCount < filesDirectoryList.length; iListCount++){
                listFileDirectory.add(new File(fileDirectory+"\\"+filesDirectoryList[iListCount]));
            }

            ZipFile zipFile = new ZipFile("C:\\CreateZIP\\FileArchive.zip");
            //Initiate Zip Parameters which define various properties
            ZipParameters parameters = new ZipParameters();
            // Set compression method to deflate compression
            parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); 
            parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); 
            //Set the encryption flag to true
            parameters.setEncryptFiles(true);
            //Set the encryption method to AES Zip Encryption
            parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);
            //file encrypted with key strength of 192, then Zip4j can decrypt this file
            parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);
            //Set password
            parameters.setPassword(stringPassword);
            // Zip the directory files
            zipFile.addFiles(listFileDirectory, parameters);
        }
        catch(ZipException ex){
            Logj.errorLog(ex);
        }
        catch(Exception ex){
            Logj.errorLog(ex);
        }
    }

提取

代码语言:javascript
运行
复制
public void extractFilesForFirmwareZip(String stringArchievedFile, String stringExtractingFilePath, String stringFileEncrypt) throws Exception{
        try{
            // Initiate ZipFile object with the path/name of the zip file.
            ZipFile zipFile = new ZipFile(stringArchievedFile);
            //Initiate Zip Parameters which define various properties
            //UnzipParameters parameters = new UnzipParameters();
            if(zipFile.isEncrypted())
                zipFile.setPassword(stringFileEncrypt);
            //Extracts all files to the path specified
            zipFile.extractAll(stringExtractingFilePath);
        }
        catch(ZipException ex){
            isValidArchiveFile = false;
            Logj.doLog(ex.getMessage(), ex);
        }
        catch(Exception ex){
            throw ex;
        }
    }
票数 1
EN

Stack Overflow用户

发布于 2016-09-24 08:50:38

您是否尝试过Zip4j站点中的示例:

代码语言:javascript
运行
复制
/*
* Copyright 2010 Srikanth Reddy Lingala  
* 
* Licensed under the Apache License, Version 2.0 (the "License"); 
* you may not use this file except in compliance with the License. 
* You may obtain a copy of the License at 
* 
* http://www.apache.org/licenses/LICENSE-2.0 
* 
* Unless required by applicable law or agreed to in writing, 
* software distributed under the License is distributed on an "AS IS" BASIS, 
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
* See the License for the specific language governing permissions and 
* limitations under the License. 
*/

package net.lingala.zip4j.examples.extract;

import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;

/**
 * Demonstrates extracting all files from a zip file
 * 
 * @author Srikanth Reddy Lingala
 *
 */
public class ExtractAllFiles {

    public ExtractAllFiles() {

        try {
            // Initiate ZipFile object with the path/name of the zip file.
            ZipFile zipFile = new ZipFile("c:\\ZipTest\\ExtractAllFiles.zip");

            // Extracts all files to the path specified
            zipFile.extractAll("c:\\ZipTest");

        } catch (ZipException e) {
            e.printStackTrace();
        }

    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        new ExtractAllFiles();
    }

}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39673776

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档