本文整理匯總了Java中net.lingala.zip4j.core.ZipFile.addFiles方法的典型用法代碼示例。如果您正苦於以下問題:Java ZipFile.addFiles方法的具體用法?Java ZipFile.addFiles怎麽用?Java ZipFile.addFiles使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類net.lingala.zip4j.core.ZipFile
的用法示例。
在下文中一共展示了ZipFile.addFiles方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: zip
import net.lingala.zip4j.core.ZipFile; //導入方法依賴的package包/類
/**
* Archives the list of files to a zip archive
*
* @param zipFileName
* @param fileList
* @param desaElement
* @throws MetsExportException
*/
private void zip(String zipFileName, ArrayList<File> fileList, IDesaElement desaElement) throws MetsExportException {
try {
File file = new File(zipFileName);
if (file.exists()) {
file.delete();
file = null;
LOG.log(Level.FINE, "File:" + zipFileName + " exists, so it was deleted");
}
ZipFile zipFile = new ZipFile(zipFileName);
ZipParameters zip4jZipParameters = new ZipParameters();
zip4jZipParameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
zip4jZipParameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
zipFile.addFiles(fileList, zip4jZipParameters);
LOG.log(Level.FINE, "Zip archive created:" + zipFileName + " for " + desaElement.getElementType());
} catch (ZipException e) {
throw new MetsExportException(desaElement.getOriginalPid(), "Unable to create a zip file:" + zipFileName, false, e);
}
}
示例2: example1
import net.lingala.zip4j.core.ZipFile; //導入方法依賴的package包/類
/**
* 示例1 創建壓縮包添加文件到壓縮包中(未設置加密)
*/
@Test
public void example1(){
try {
ZipFile zipFile = new ZipFile("src/main/resources/AddFilesDeflateComp.zip");
ArrayList<File> filesToAdd = new ArrayList<File>();
filesToAdd.add(new File("src/main/resources/sample.txt"));
filesToAdd.add(new File("src/main/resources/zip4j.txt"));
filesToAdd.add(new File("src/main/resources/zip4j-1.3.2.jar"));
ZipParameters parameters = new ZipParameters();
parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); // set compression method to deflate compression
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
zipFile.addFiles(filesToAdd, parameters);
CompressUtil.printZipInfo("src/main/resources/AddFilesDeflateComp.zip");
} catch (ZipException e) {
e.printStackTrace();
}
}
示例3: example2
import net.lingala.zip4j.core.ZipFile; //導入方法依賴的package包/類
/**
* 示例2 創建壓縮包添加文件到指定目錄中進行壓縮
*/
@Test
public void example2(){
try {
ZipFile zipFile = new ZipFile("src/main/resources/AddFilesDeflateComp.zip");
ArrayList<File> filesToAdd = new ArrayList<File>();
filesToAdd.add(new File("src/main/resources/sample.txt"));
filesToAdd.add(new File("src/main/resources/zip4j.txt"));
filesToAdd.add(new File("src/main/resources/zip4j-1.3.2.jar"));
ZipParameters parameters = new ZipParameters();
parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
parameters.setRootFolderInZip("target/");
zipFile.addFiles(filesToAdd, parameters);
} catch (ZipException e) {
e.printStackTrace();
}
}
示例4: zip
import net.lingala.zip4j.core.ZipFile; //導入方法依賴的package包/類
/**
* Adds a directory to a ZIP-archive.
*
* @param source directory to compress, will not be added itself;
* source directory child files will be placed in the root of archive
* @param destination ZIP-archive
* @param level compression level (0-9)
* @param skipFilter skipped files filter or {@code null} to accept all files
* @throws IOException if any I/O-exception occured
*/
public static void zip(File source, File destination, int level, @Nullable FileFilter skipFilter)
throws IOException {
try {
ZipParameters parameters = new ZipParameters();
parameters.setIncludeRootFolder(false);
parameters.setCompressionLevel(level);
parameters.setReadHiddenFiles(true);
parameters.setDefaultFolderPath(source.getAbsolutePath());
ZipFile zipFile = new ZipFile(destination);
zipFile.addFiles(deepListFilesInDirectory(source, skipFilter, !parameters.isReadHiddenFiles()), parameters);
} catch (ZipException e) {
throw new IOException("Can't add directory to ZIP-file.", e);
}
}
示例5: zip
import net.lingala.zip4j.core.ZipFile; //導入方法依賴的package包/類
/**
* 使用給定密碼壓縮指定文件或文件夾到指定位置.
* <p>
* dest可傳最終壓縮文件存放的絕對路徑,也可以傳存放目錄,也可以傳null或者"".<br />
* 如果傳null或者""則將壓縮文件存放在當前目錄,即跟源文件同目錄,壓縮文件名取源文件名,以.zip為後綴;<br />
* 如果以路徑分隔符(File.separator)結尾,則視為目錄,壓縮文件名取源文件名,以.zip為後綴,否則視為文件名.
* @param src 要壓縮的文件或文件夾路徑
* @param dest 壓縮文件存放路徑
* @param isCreateDir 是否在壓縮文件裏創建目錄,僅在壓縮文件為目錄時有效.<br />
* 如果為false,將直接壓縮目錄下文件到壓縮文件.
* @param passwd 壓縮使用的密碼
* @return 最終的壓縮文件存放的絕對路徑,如果為null則說明壓縮失敗.
*/
public static String zip(String src, String dest, boolean isCreateDir, String passwd) {
File srcFile = new File(src);
File destFile = new File(dest);
dest = buildDestinationZipFilePath(srcFile, dest);
ZipParameters parameters = new ZipParameters();
parameters.setFileNameInZip(destFile.getName().substring(0,destFile.getName().lastIndexOf("."))+".log");
parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); // 壓縮方式
parameters.setSourceExternalStream(true);
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); // 壓縮級別
if (!TextUtils.isEmpty(passwd)) {
parameters.setEncryptFiles(true);
parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD); // 加密方式
parameters.setPassword(passwd.toCharArray());
}
try {
ZipFile zipFile = new ZipFile(dest);
if (srcFile.isDirectory()) {
// 如果不創建目錄的話,將直接把給定目錄下的文件壓縮到壓縮文件,即沒有目錄結構
if (!isCreateDir) {
File [] subFiles = srcFile.listFiles();
ArrayList<File> temp = new ArrayList<File>();
Collections.addAll(temp, subFiles);
zipFile.addFiles(temp, parameters);
return dest;
}
zipFile.addFolder(srcFile, parameters);
} else {
zipFile.addFile(srcFile, parameters);
}
return dest;
} catch (ZipException e) {
e.printStackTrace();
}
return null;
}
示例6: zip
import net.lingala.zip4j.core.ZipFile; //導入方法依賴的package包/類
/**
* 使用給定密碼壓縮指定文件或文件夾到指定位置.
* <p>
* dest可傳最終壓縮文件存放的絕對路徑,也可以傳存放目錄,也可以傳null或者"".<br />
* 如果傳null或者""則將壓縮文件存放在當前目錄,即跟源文件同目錄,壓縮文件名取源文件名,以.zip為後綴;<br />
* 如果以路徑分隔符(File.separator)結尾,則視為目錄,壓縮文件名取源文件名,以.zip為後綴,否則視為文件名.
* @param src 要壓縮的文件或文件夾路徑
* @param dest 壓縮文件存放路徑
* @param isCreateDir 是否在壓縮文件裏創建目錄,僅在壓縮文件為目錄時有效.<br />
* 如果為false,將直接壓縮目錄下文件到壓縮文件.
* @param passwd 壓縮使用的密碼
* @return 最終的壓縮文件存放的絕對路徑,如果為null則說明壓縮失敗.
*/
public static String zip(String src, String dest, boolean isCreateDir, String passwd) {
File srcFile = new File(src);
dest = buildDestinationZipFilePath(srcFile, dest);
ZipParameters parameters = new ZipParameters();
parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); // 壓縮方式
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); // 壓縮級別
if (!StringUtils.isEmpty(passwd)) {
parameters.setEncryptFiles(true);
parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD); // 加密方式
parameters.setPassword(passwd.toCharArray());
}
try {
ZipFile zipFile = new ZipFile(dest);
if (srcFile.isDirectory()) {
// 如果不創建目錄的話,將直接把給定目錄下的文件壓縮到壓縮文件,即沒有目錄結構
if (!isCreateDir) {
File [] subFiles = srcFile.listFiles();
ArrayList<File> temp = new ArrayList<File>();
Collections.addAll(temp, subFiles);
zipFile.addFiles(temp, parameters);
return dest;
}
zipFile.addFolder(srcFile, parameters);
} else {
zipFile.addFile(srcFile, parameters);
}
return dest;
} catch (ZipException e) {
e.printStackTrace();
}
return null;
}
示例7: AddFilesStoreComp
import net.lingala.zip4j.core.ZipFile; //導入方法依賴的package包/類
public AddFilesStoreComp() {
try {
// Initiate ZipFile object with the path/name of the zip file
// Zip file may not necessarily exist. If zip file exists, then
// all these files are added to the zip file. If zip file does not
// exist, then a new zip file is created with the files mentioned
ZipFile zipFile = new ZipFile("c:\\ZipTest\\AddFilesStoreComp.zip");
// Build the list of files to be added in the array list
// Objects of type File have to be added to the ArrayList
ArrayList filesToAdd = new ArrayList();
filesToAdd.add(new File("c:\\ZipTest\\sample.txt"));
filesToAdd.add(new File("c:\\ZipTest\\myvideo.avi"));
filesToAdd.add(new File("c:\\ZipTest\\mysong.mp3"));
// Initiate Zip Parameters which define various properties such
// as compression method, etc. More parameters are explained in other
// examples
ZipParameters parameters = new ZipParameters();
parameters.setCompressionMethod(Zip4jConstants.COMP_STORE); // set compression method to store compression
// Now add files to the zip file
// Note: To add a single file, the method addFile can be used
// Note: If the zip file already exists and if this zip file is a split file
// then this method throws an exception as Zip Format Specification does not
// allow updating split zip files
zipFile.addFiles(filesToAdd, parameters);
} catch (ZipException e) {
e.printStackTrace();
}
}
示例8: AddFilesToFolderInZip
import net.lingala.zip4j.core.ZipFile; //導入方法依賴的package包/類
public AddFilesToFolderInZip() {
try {
ZipFile zipFile = new ZipFile("c:\\ZipTest\\AddFilesDeflateComp.zip");
// Build the list of files to be added in the array list
ArrayList filesToAdd = new ArrayList();
filesToAdd.add(new File("c:\\ZipTest\\sample.txt"));
filesToAdd.add(new File("c:\\ZipTest\\myvideo.avi"));
filesToAdd.add(new File("c:\\ZipTest\\mysong.mp3"));
// Initiate Zip Parameters
ZipParameters parameters = new ZipParameters();
parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); // set compression method to deflate compression
// Set the compression level.
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
// Sets the folder in the zip file to which these new files will be added.
// In this example, test2 is the folder to which these files will be added.
// Another example: if files were to be added to a directory test2/test3, then
// below statement should be parameters.setRootFolderInZip("test2/test3/");
parameters.setRootFolderInZip("test2/");
// Now add files to the zip file
zipFile.addFiles(filesToAdd, parameters);
} catch (ZipException e) {
e.printStackTrace();
}
}
示例9: zip
import net.lingala.zip4j.core.ZipFile; //導入方法依賴的package包/類
/**
* 使用給定密碼壓縮指定文件或文件夾到指定位置.
* <p>
* dest可傳最終壓縮文件存放的絕對路徑,也可以傳存放目錄,也可以傳null或者"".<br />
* 如果傳null或者""則將壓縮文件存放在當前目錄,即跟源文件同目錄,壓縮文件名取源文件名,以.zip為後綴;<br />
* 如果以路徑分隔符(File.separator)結尾,則視為目錄,壓縮文件名取源文件名,以.zip為後綴,否則視為文件名.
* @param src 要壓縮的文件或文件夾路徑
* @param dest 壓縮文件存放路徑
* @param isCreateDir 是否在壓縮文件裏創建目錄,僅在壓縮文件為目錄時有效.<br />
* 如果為false,將直接壓縮目錄下文件到壓縮文件.
* @param passwd 壓縮使用的密碼
* @return 最終的壓縮文件存放的絕對路徑,如果為null則說明壓縮失敗.
*/
public static String zip(String src, String dest, boolean isCreateDir, String passwd) {
File srcFile = new File(src);
dest = buildDestinationZipFilePath(srcFile, dest);
ZipParameters parameters = new ZipParameters();
parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); // 壓縮方式
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); // 壓縮級別
if (!StringUtils.isEmpty(passwd)) {
parameters.setEncryptFiles(true);
parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD); // 加密方式
parameters.setPassword(passwd.toCharArray());
}
try {
ZipFile zipFile = new ZipFile(dest);
zipFile.setFileNameCharset(Constants.GBK); //不可隨意更換位置
if (srcFile.isDirectory()) {
// 如果不創建目錄的話,將直接把給定目錄下的文件壓縮到壓縮文件,即沒有目錄結構
if (!isCreateDir) {
File [] subFiles = srcFile.listFiles();
ArrayList<File> temp = new ArrayList<File>();
if(null != subFiles && subFiles.length > 0)
Collections.addAll(temp, subFiles);
zipFile.addFiles(temp, parameters);
return dest;
}
zipFile.addFolder(srcFile, parameters);
} else {
zipFile.addFile(srcFile, parameters);
}
return dest;
} catch (ZipException e) {
Exceptions.printException(e);;
}
return null;
}
示例10: main
import net.lingala.zip4j.core.ZipFile; //導入方法依賴的package包/類
public static void main(String[] args) throws IOException, ZipException {
ZipFile zipFile = new ZipFile("test1.zip");
ArrayList<File> files = new ArrayList<File>();
files.add(new File("conf.json"));
ZipParameters parameters = new ZipParameters();
zipFile.addFiles(files, parameters);
}
示例11: writeZip
import net.lingala.zip4j.core.ZipFile; //導入方法依賴的package包/類
void writeZip(File pkgFile, File packageFolder) throws ZipException {
ZipFile zipFile = new ZipFile(pkgFile);
ZipParameters zipParameters = new ZipParameters();
zipParameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD);
zipParameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
zipParameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
zipParameters.setIncludeRootFolder(false);
zipParameters.setDefaultFolderPath(packageFolder.getAbsolutePath());
zipFile.addFiles(listZipFiles(packageFolder), zipParameters);
}
示例12: archiveDir
import net.lingala.zip4j.core.ZipFile; //導入方法依賴的package包/類
private void archiveDir(String path) {
try {
// Initiate ZipFile object with the path/name of the zip file.
String zipname = g.getSelectedAppName();
ZipFile zipFile = new ZipFile(Environment.getExternalStorageDirectory() + "/ChromeAPKS/" + zipname + ".zip");
// Folder to add
String folderToAdd = path;
// Initiate Zip Parameters which define various properties such
// as compression method, etc.
ZipParameters parameters = new ZipParameters();
// set compression method to store compression
parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
// Set the compression level
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
// Add folder to the zip file
zipFile.addFolder(folderToAdd, parameters);
ArrayList filesToAdd = new ArrayList();
filesToAdd.add(new File(path + "app_main.html"));
filesToAdd.add(new File(path + "icon.png"));
zipFile.addFiles(filesToAdd, parameters);
} catch (Exception e) {
e.printStackTrace();
}
}
示例13: example4
import net.lingala.zip4j.core.ZipFile; //導入方法依賴的package包/類
/**
* 示例4 創建加密壓縮包
*/
@Test
public void example4(){
try {
ZipFile zipFile = new ZipFile("src/main/resources/AddFilesWithAESZipEncryption.zip");
ArrayList<File> filesToAdd = new ArrayList<File>();
filesToAdd.add(new File("src/main/resources/sample.txt"));
filesToAdd.add(new File("src/main/resources/zip4j.txt"));
filesToAdd.add(new File("src/main/resources/zip4j-1.3.2.jar"));
ZipParameters parameters = new ZipParameters();
parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
parameters.setEncryptFiles(true);
parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);
parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);
parameters.setPassword("123456");
zipFile.addFiles(filesToAdd, parameters);
} catch (ZipException e) {
e.printStackTrace();
}
}
示例14: zip
import net.lingala.zip4j.core.ZipFile; //導入方法依賴的package包/類
/**
* 使用給定密碼壓縮指定文件或文件夾到指定位置.
* <p>
* dest可傳最終壓縮文件存放的絕對路徑,也可以傳存放目錄,也可以傳null或者"".<br />
* 如果傳null或者""則將壓縮文件存放在當前目錄,即跟源文件同目錄,壓縮文件名取源文件名,以.zip為後綴;<br />
* 如果以路徑分隔符(File.separator)結尾,則視為目錄,壓縮文件名取源文件名,以.zip為後綴,否則視為文件名.
*
* @param src
* 要壓縮的文件或文件夾路徑
* @param dest
* 壓縮文件存放路徑
* @param isCreateDir
* 是否在壓縮文件裏創建目錄,僅在壓縮文件為目錄時有效.<br />
* 如果為false,將直接壓縮目錄下文件到壓縮文件.
* @param passwd
* 壓縮使用的密碼
* @return 最終的壓縮文件存放的絕對路徑,如果為null則說明壓縮失敗.
*/
public static String zip(String src, String dest, boolean isCreateDir,
String passwd) {
File srcFile = new File(src);
dest = buildDestinationZipFilePath(srcFile, dest);
ZipParameters parameters = new ZipParameters();
parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); // 壓縮方式
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); // 壓縮級別
if (!StringUtils.isEmpty(passwd)) {
parameters.setEncryptFiles(true);
parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD); // 加密方式
parameters.setPassword(passwd.toCharArray());
}
try {
ZipFile zipFile = new ZipFile(dest);
if (srcFile.isDirectory()) {
// 如果不創建目錄的話,將直接把給定目錄下的文件壓縮到壓縮文件,即沒有目錄結構
if (!isCreateDir) {
File[] subFiles = srcFile.listFiles();
ArrayList<File> temp = new ArrayList<File>();
Collections.addAll(temp, subFiles);
zipFile.addFiles(temp, parameters);
return dest;
}
zipFile.addFolder(srcFile, parameters);
} else {
zipFile.addFile(srcFile, parameters);
}
return dest;
} catch (ZipException e) {
e.printStackTrace();
}
return null;
}
示例15: addFiles
import net.lingala.zip4j.core.ZipFile; //導入方法依賴的package包/類
@Override
public void addFiles(File zip, List<File> files, String password, DeflateLevel defLevel)
throws ZipException {
try {
ZipFile zipFile = new ZipFile(zip);
zipFile.addFiles(new ArrayList<>(files), getParams(password, null, defLevel));
} catch (net.lingala.zip4j.exception.ZipException e) {
throw new ZipException(e);
}
}