在java中提取ZIP
各位 我正在使用zip4jAPI提取java中的.zip文件,并且能够提取这些文件在java中提取ZIP,java,servlets,zip4j,Java,Servlets,Zip4j,各位 我正在使用zip4jAPI提取java中的.zip文件,并且能够提取这些文件 我曾经用zip压缩整个目录,使之成为zip,它包含文件和嵌套目录,使用 addFolder(fileDirectory,参数)//ZIP目录文件/文件夹 使用 ZipFile zipFile = new ZipFile(stringArchievedFile); //Extracts all files to the path specified zipFile.extractAll(stringExtracti
ZipFile zipFile = new ZipFile(stringArchievedFile);
//Extracts all files to the path specified
zipFile.extractAll(stringExtractingFilePath);
zipFile.extractAll(path)
方法提供的路径,但是正在创建一个以上的目录。如何将文件解压缩到实际指定的目录
比如:
提取路径
C:\ExtractionPath
文件路径
C:\SelectingPath\File1
C:\SelectingPath\File2
C:\SelectingPath\Directory1\File1
C:\SelectingPath\Directory2\File1
我将选择C:\SelectingPath目录进行压缩并
我将选择C:\ExtractionPath目录来提取文件
提取后,所有提取的文件将进入
**C:\ExtractionPath\SelectingPath**
我需要目录中的所有文件
**C:\ExtractionPath**
本身
请帮我解决这个问题
提前感谢您是否尝试以下示例:
/*
* 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();
}
}
您是否尝试过以下示例:
/*
* 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();
}
}
谢谢Andreas&esprittn 我们需要将
ArrayList
作为参数传递给addFiles(ArrayList,ZipParameters)
方法,以便归档目录的整个目录内容。我得到了预期的结果
遵循存档代码流:
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);
}
}
谢谢Andreas&esprittn 我们需要将
ArrayList
作为参数传递给addFiles(ArrayList,ZipParameters)
方法,以便归档目录的整个目录内容。我得到了预期的结果
遵循存档代码流:
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);
}
}
不要压缩
SelectingPath
文件夹。压缩内容。如果打开zip文件,您会看到所有文件的路径都以SelectingPath
开头,您不希望这样。您的意思是,我需要将所有文件和目录添加到ArrayList
中,并将其作为参数传递给zipFile.addFiles()方法,作为zipFile.addFiles(arrayListFilesFolders,parameters)
?@Andreas,如果我对文件和目录都使用ArrayList,并将其作为参数传递给zipFile.addFile()方法,我将获得以下异常,并且无法存档我选择的完整目录ex=(net.lingala.zip4j.exception.ZipException)net.lingala.zip4j.exception.zipeexception:input ArrayList中的一个或多个元素不是File类型
不要压缩SelectingPath
文件夹。压缩内容。如果打开zip文件,您会看到所有文件的路径都以SelectingPath
开头,您不希望这样。您的意思是,我需要将所有文件和目录添加到ArrayList
中,并将其作为参数传递给zipFile.addFiles()方法,作为zipFile.addFiles(arrayListFilesFolders,parameters)
?@Andreas,如果我对文件和目录都使用ArrayList,并将其作为参数传递给zipFile.addFile()方法,我将获得以下异常,并且无法存档我选择的完整目录ex=(net.lingala.zip4j.exception.ZipException)net.lingala.zip4j.exception.zipeexception:输入数组列表中的一个或多个元素不是File类型
Yes。我也用了这个例子。但我面临的问题是一样的。我也用了这个例子。但我面临的问题是一样的。