Java Code Examples for net.lingala.zip4j.model.ZipParameters#setCompressionLevel()

The following examples show how to use net.lingala.zip4j.model.ZipParameters#setCompressionLevel() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Zip4j.java    From maven-framework-project with MIT License 6 votes vote down vote up
/**
 * 示例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();
       } 
}
 
Example 2
Source File: CollectEarthUtils.java    From collect-earth with MIT License 6 votes vote down vote up
public static ZipFile addFileToZip(String fileToCompress, File srcFile, String fileNameInZip )
		throws ZipException,IOException{
	File destBackupFile = new File( fileToCompress );	
	ZipFile zipBackupFile = new ZipFile( destBackupFile );

	ZipParameters zipParameters = new ZipParameters();
	// COMP_DEFLATE is for compression
	zipParameters.setCompressionMethod(CompressionMethod.DEFLATE);
	// DEFLATE_LEVEL_ULTRA = maximum compression
	zipParameters.setCompressionLevel(CompressionLevel.ULTRA);
	// deprecated in version 2 of lingala ?? does it matter? zipParameters.setSourceExternalStream(true);
	zipParameters.setFileNameInZip( fileNameInZip );				
	zipBackupFile.addFile(srcFile, zipParameters);

	return zipBackupFile;
}
 
Example 3
Source File: DesaElementVisitor.java    From proarc with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 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);
    }

}
 
Example 4
Source File: ZipManager.java    From AndroidZip with Apache License 2.0 6 votes vote down vote up
/**
 * 压缩多个文件
 *
 * @param list                被压缩的文件集合
 * @param destinationFilePath 压缩后生成的文件路径
 * @param password            压缩 密码
 * @param callback            回调
 */
public static void zip(ArrayList<File> list, String destinationFilePath, String password, final IZipCallback callback) {
    if (list == null || list.size() == 0 || !Zip4jUtil.isStringNotNullAndNotEmpty(destinationFilePath)) {
        if (callback != null) callback.onFinish(false);
        return;
    }
    ZipLog.debug("zip: list=" + list.toString() + " , destinationFilePath=" + destinationFilePath + " , password=" + password);
    try {
        ZipParameters parameters = new ZipParameters();
        parameters.setCompressionMethod(8);
        parameters.setCompressionLevel(5);
        if (password.length() > 0) {
            parameters.setEncryptFiles(true);
            parameters.setEncryptionMethod(99);
            parameters.setAesKeyStrength(3);
            parameters.setPassword(password);
        }
        ZipFile zipFile = new ZipFile(destinationFilePath);
        zipFile.setRunInThread(true);
        zipFile.addFiles(list, parameters);
        timerMsg(callback, zipFile);
    } catch (Exception e) {
        if (callback != null) callback.onFinish(false);
        ZipLog.debug("zip: Exception=" + e.getMessage());
    }
}
 
Example 5
Source File: CompressionHandler.java    From butterfly with MIT License 6 votes vote down vote up
void compress(TransformationResult transformationResult) {
    File inputFile = transformationResult.getTransformedApplicationDir().getAbsoluteFile();
    File compressedFile = new File(transformationResult.getTransformedApplicationDir().getAbsolutePath() + ".zip");

    logger.info("Compressing transformed application");

    try {
        ZipFile zipFile = new ZipFile(compressedFile);
        ZipParameters parameters = new ZipParameters();

        parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
        parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);

        zipFile.addFolder(inputFile, parameters);
        FileUtils.deleteDirectory(transformationResult.getTransformedApplicationDir());

        logger.info("Transformed application has been compressed to {}", compressedFile.getAbsoluteFile());
    } catch (Exception e) {
        logger.error("An exception happened when compressing transformed application", e);
    }
}
 
Example 6
Source File: BackupAppsTasker.java    From KAM with GNU General Public License v3.0 6 votes vote down vote up
private void withData(AppsModel model, ApplicationInfo packageInfo, File file, FileUtil fileUtil, int count) throws Exception {
    File dataFolder = new File(packageInfo.dataDir);
    File dataFile = new File(fileUtil.getBaseFolderName() + model.getPackageName());
    boolean z = RootManager.getInstance().copyFile(dataFolder.getPath(), fileUtil.getBaseFolderName());
    ZipParameters parameters = new ZipParameters();
    parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
    parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_FASTEST);
    if (z) {
        if (dataFile.exists()) {
            zipData.addFolder(dataFile, parameters);
            FileUtils.deleteDirectory(dataFile);
        }
    }
    ProgressModel progressModel = new ProgressModel();
    progressModel.setProgress(count);
    progressModel.setFileName(model.getAppName());
    publishProgress(progressModel);
}
 
Example 7
Source File: ZipArchive.java    From Mzip-Android with Apache License 2.0 6 votes vote down vote up
public static void zip(String targetPath, String destinationFilePath, String password) {
    try {
        ZipParameters parameters = new ZipParameters();
        parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
        parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);

        if (password.length() > 0) {
            parameters.setEncryptFiles(true);
            parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);
            parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);
            parameters.setPassword(password);
        }

        ZipFile zipFile = new ZipFile(destinationFilePath);

        File targetFile = new File(targetPath);
        if (targetFile.isFile()) {
            zipFile.addFile(targetFile, parameters);
        } else if (targetFile.isDirectory()) {
            zipFile.addFolder(targetFile, parameters);
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 8
Source File: ZipUtil.java    From MonitorClient with Apache License 2.0 6 votes vote down vote up
/**
 * 添加文件夹到zip中
 * @data 2017年4月18日
 * @param inPath
 * @param storagePath
 * @param outPath
 * @param password
 * @return
 */
public static boolean addFoldInZip(String inPath,String storagePath,String outPath,String password) {
	try {
		ZipFile zipFile = new ZipFile(outPath);  
		ZipParameters parameters = new ZipParameters();       
		parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);         
		parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
		parameters.setRootFolderInZip(storagePath);  ;  
		if(password!=null&&!password.equals("")){
			parameters.setEncryptFiles(true);  
			parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);  
			parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);  
			parameters.setPassword(password);  
		}
		zipFile.addFolder(inPath, parameters);  		
		return true;
	} catch (ZipException e) {
		e.printStackTrace();
		return false;
	}
}
 
Example 9
Source File: ZipUtil.java    From MonitorClient with Apache License 2.0 6 votes vote down vote up
/**
 * 添加多个文件到zip中指定的文件夹中
 * @param inPath
 * @param storagePath
 * @param outPath
 * @param password
 * @return
 */
public static boolean addFilesInZip(ArrayList<File> inFiles,String storagePath,String outPath,String password) {
	try {
		ArrayList filesToAdd = new ArrayList();  
		ZipFile zipFile = new ZipFile(outPath);
		ZipParameters parameters = new ZipParameters();
		parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); // set compression method to deflate compression
		parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); 
		parameters.setRootFolderInZip(storagePath);  
		if(password!=null&&!password.equals("")){
			parameters.setEncryptFiles(true);  
			parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);  
			parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);  
			parameters.setPassword(password); 
		}
		zipFile.addFiles(inFiles, parameters);
		return true;
	} catch (ZipException e) {
		e.printStackTrace();
		return false;
	}
}
 
Example 10
Source File: CejshBuilder.java    From proarc with GNU General Public License v3.0 5 votes vote down vote up
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);
}
 
Example 11
Source File: ZipUtilities.java    From openemm with GNU Affero General Public License v3.0 5 votes vote down vote up
public static void compressToEncryptedZipFile(File destinationZipFile, File fileToZip, String fileNameInZip, String zipPassword) throws Exception {
	net.lingala.zip4j.core.ZipFile zipFile = new net.lingala.zip4j.core.ZipFile(destinationZipFile);
	ZipParameters parameters = new ZipParameters();
	parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
	parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
	parameters.setEncryptFiles(true);
	parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD);
	if (StringUtils.isNotBlank(fileNameInZip)) {
		parameters.setFileNameInZip(fileNameInZip);
	}
	parameters.setPassword(zipPassword.toCharArray());
	zipFile.addFile(fileToZip, parameters);
}
 
Example 12
Source File: PdcaReportExcelCommand.java    From bamboobsc with Apache License 2.0 5 votes vote down vote up
private String createZipFile(List<SysUploadVO> uploads) throws Exception {
	ZipFile zip = new ZipFile( Constants.getWorkTmpDir() + SimpleUtils.getUUIDStr() + ".zip" );
	ZipParameters parameters = new ZipParameters();
	parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
	parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
	for (SysUploadVO reportUpload : uploads) {
		zip.addFile(UploadSupportUtils.getRealFile(reportUpload.getOid()), parameters);
	}
	return UploadSupportUtils.create(Constants.getSystem(), UploadTypes.IS_TEMP, false, zip.getFile(), "pdca-report.zip");
}
 
Example 13
Source File: CompressUtil.java    From maven-framework-project with MIT License 5 votes vote down vote up
/**
 * 使用给定密码压缩指定文件或文件夹到指定位置.
 * <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;
}
 
Example 14
Source File: FileIO.java    From PHONK with GNU General Public License v3.0 5 votes vote down vote up
static public void zipFolder(String src, String dst) throws Exception {
    File f = new File(dst);
    //make dirs if necessary
    f.getParentFile().mkdirs();

    ZipFile zipfile = new ZipFile(f.getAbsolutePath());
    ZipParameters parameters = new ZipParameters();
    parameters.setIncludeRootFolder(false);
    parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
    parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
    zipfile.addFolder(src, parameters);
}
 
Example 15
Source File: CollectEarthUtils.java    From collect-earth with MIT License 5 votes vote down vote up
public static void addFolderToZip(ZipFile zipFile, File folderToCompress )
		throws ZipException {
	ZipParameters zipParameters = new ZipParameters();
	// COMP_DEFLATE is for compression
	zipParameters.setCompressionMethod(CompressionMethod.DEFLATE);
	// DEFLATE_LEVEL_ULTRA = maximum compression
	zipParameters.setCompressionLevel(CompressionLevel.ULTRA);

	if( folderToCompress.exists() && folderToCompress.isDirectory() ){
		zipFile.addFolder(folderToCompress, zipParameters);
	}
}
 
Example 16
Source File: RxZipTool.java    From RxTools-master with Apache License 2.0 5 votes vote down vote up
public static String zipEncrypt(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 (!RxDataTool.isNullString(passwd)) {
        parameters.setEncryptFiles(true);
        parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD); // 加密方式
        parameters.setPassword(passwd.toCharArray());
    }
    try {
        net.lingala.zip4j.core.ZipFile zipFile = new net.lingala.zip4j.core.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;
}
 
Example 17
Source File: ZipManager.java    From AndroidZip with Apache License 2.0 5 votes vote down vote up
/**
 * 压缩文件或者文件夹
 *
 * @param targetPath          被压缩的文件路径
 * @param destinationFilePath 压缩后生成的文件路径
 * @param password            压缩加密 密码
 * @param callback            压缩进度回调
 */
public static void zip(String targetPath, String destinationFilePath, String password, IZipCallback callback) {
    if (!Zip4jUtil.isStringNotNullAndNotEmpty(targetPath) || !Zip4jUtil.isStringNotNullAndNotEmpty(destinationFilePath)) {
        if (callback != null) callback.onFinish(false);
        return;
    }
    ZipLog.debug("zip: targetPath=" + targetPath + " , destinationFilePath=" + destinationFilePath + " , password=" + password);
    try {
        ZipParameters parameters = new ZipParameters();
        parameters.setCompressionMethod(8);
        parameters.setCompressionLevel(5);
        if (password.length() > 0) {
            parameters.setEncryptFiles(true);
            parameters.setEncryptionMethod(99);
            parameters.setAesKeyStrength(3);
            parameters.setPassword(password);
        }
        ZipFile zipFile = new ZipFile(destinationFilePath);
        zipFile.setRunInThread(true);
        File targetFile = new File(targetPath);
        if (targetFile.isDirectory()) {
            zipFile.addFolder(targetFile, parameters);
        } else {
            zipFile.addFile(targetFile, parameters);
        }
        timerMsg(callback, zipFile);
    } catch (Exception e) {
        if (callback != null) callback.onFinish(false);
        ZipLog.debug("zip: Exception=" + e.getMessage());
    }
}
 
Example 18
Source File: Zip4j.java    From maven-framework-project with MIT License 5 votes vote down vote up
/**
 * 示例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();
       }
}
 
Example 19
Source File: FileHeaderFactoryTest.java    From zip4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testGenerateFileHeaderWithCompressionLeveUltra() throws ZipException {
  ZipParameters zipParameters = generateZipParameters();
  zipParameters.setCompressionLevel(CompressionLevel.ULTRA);

  FileHeader fileHeader = fileHeaderFactory.generateFileHeader(zipParameters, false, 0, InternalZipConstants.CHARSET_UTF_8, rawIO);

  verifyCompressionLevelGridForDeflate(CompressionLevel.ULTRA, fileHeader.getGeneralPurposeFlag()[0]);
}
 
Example 20
Source File: BackupAppsTasker.java    From KAM with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected ProgressModel doInBackground(Void... params) {
    List<AppsModel> appsModelList;
    if (appsModels == null) {
        appsModelList = new AppListCreator(context).getAppList();
    } else {
        appsModelList = appsModels;
    }
    if (appsModelList == null) {
        return onError("Apps Are Empty, Please select ones");
    }
    ProgressModel progressModel = new ProgressModel();
    progressModel.setMax(appsModelList.size());
    publishProgress(progressModel);
    try {
        FileUtil fileUtil = new FileUtil();
        File zipFileName = new File(fileUtil.getBaseFolderName() + "backup.zip");
        File zipFileData = new File(fileUtil.getBaseFolderName() + "data.zip");
        zipFile = new ZipFile(zipFileName);
        zipData = new ZipFile(zipFileData);
        if (!zipData.isValidZipFile()) {
            if (zipData.getFile() != null && zipData.getFile().exists())
                zipData.getFile().delete();
        }
        if (!zipFile.isValidZipFile()) {
            if (zipFile.getFile() != null && zipFile.getFile().exists())
                zipFile.getFile().delete();
        }
        int count = 0;
        boolean withData = AppHelper.isBackupData(context);
        if (withData) RootManager.getInstance().obtainPermission();
        for (AppsModel model : appsModelList) {
            if (isCancelled()) {
                return onError("cancelled");
            }
            if (model != null) {
                ApplicationInfo packageInfo = context.getPackageManager().getApplicationInfo(model.getPackageName(), 0);
                File file = new File(packageInfo.sourceDir);
                if (file.exists()) {
                    count++;
                    File fileToSave = fileUtil.generateFile(model.getAppName());
                    progressModel = new ProgressModel();
                    progressModel.setProgress(count);
                    progressModel.setFileName(fileToSave.getName());
                    publishProgress(progressModel);
                    ZipParameters parameters = new ZipParameters();
                    parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
                    parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_FASTEST);
                    parameters.setSourceExternalStream(true);
                    parameters.setFileNameInZip(fileToSave.getName().replaceAll(" ", ""));
                    zipFile.addFile(file, parameters);
                    progressModel = new ProgressModel();
                    progressModel.setProgress(count);
                    progressModel.setFileName(fileToSave.getName());
                    publishProgress(progressModel);
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        return onError(e.getMessage());
    }

    return null;
}