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

The following examples show how to use net.lingala.zip4j.model.ZipParameters#setAesKeyStrength() . 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: 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 2
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 addFileInZip(String inPath,String storagePath,String outPath,String password) {
	try {

		ZipFile zipFile = new ZipFile(outPath);
		File inFile = new File(inPath);
		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.addFile(inFile, parameters);
		return true;
	} catch (ZipException e) {
		e.printStackTrace();
		return false;
	}
}
 
Example 3
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 4
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 5
Source File: ZipInputStreamIT.java    From zip4j with Apache License 2.0 6 votes vote down vote up
private File createZipFile(CompressionMethod compressionMethod, boolean encryptFiles,
                           EncryptionMethod encryptionMethod, AesKeyStrength aesKeyStrength, char[] password,
                           AesVersion aesVersion)
    throws IOException {

  File outputFile = temporaryFolder.newFile("output.zip");
  deleteFileIfExists(outputFile);

  ZipFile zipFile = new ZipFile(outputFile, password);

  ZipParameters zipParameters = new ZipParameters();
  zipParameters.setCompressionMethod(compressionMethod);
  zipParameters.setEncryptFiles(encryptFiles);
  zipParameters.setEncryptionMethod(encryptionMethod);
  zipParameters.setAesKeyStrength(aesKeyStrength);
  zipParameters.setAesVersion(aesVersion);

  zipFile.addFiles(AbstractIT.FILES_TO_ADD, zipParameters);

  return outputFile;
}
 
Example 6
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 7
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 8
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 9
Source File: ZipEngine.java    From AndroidZip with Apache License 2.0 5 votes vote down vote up
private void checkParameters(ZipParameters parameters) throws ZipException {
	
	if (parameters == null) {
		throw new ZipException("cannot validate zip parameters");
	}
	
	if ((parameters.getCompressionMethod() != Zip4jConstants.COMP_STORE) && 
			parameters.getCompressionMethod() != Zip4jConstants.COMP_DEFLATE) {
		throw new ZipException("unsupported compression type");
	}
	
	if (parameters.getCompressionMethod() == Zip4jConstants.COMP_DEFLATE) {
		if (parameters.getCompressionLevel() < 0 && parameters.getCompressionLevel() > 9) {
			throw new ZipException("invalid compression level. compression level dor deflate should be in the range of 0-9");
		}
	}
	
	if (parameters.isEncryptFiles()) {
		if (parameters.getEncryptionMethod() != Zip4jConstants.ENC_METHOD_STANDARD && 
				parameters.getEncryptionMethod() != Zip4jConstants.ENC_METHOD_AES) {
			throw new ZipException("unsupported encryption method");
		}
		
		if (parameters.getPassword() == null || parameters.getPassword().length <= 0) {
			throw new ZipException("input password is empty or null");
		}
	} else {
		parameters.setAesKeyStrength(-1);
		parameters.setEncryptionMethod(-1);
	}
	
}
 
Example 10
Source File: FileHeaderFactoryTest.java    From zip4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testGenerateFileHeaderWithAesEncryptionWithNullKeyStrengthThrowsException() throws ZipException {
  expectedException.expect(ZipException.class);
  expectedException.expectMessage("invalid AES key strength");

  ZipParameters zipParameters = generateZipParameters();
  zipParameters.setEncryptFiles(true);
  zipParameters.setEncryptionMethod(EncryptionMethod.AES);
  zipParameters.setAesKeyStrength(null);

  fileHeaderFactory.generateFileHeader(zipParameters, false, 0, InternalZipConstants.CHARSET_UTF_8, rawIO);
}
 
Example 11
Source File: ZipOutputStreamIT.java    From zip4j with Apache License 2.0 5 votes vote down vote up
private ZipParameters buildZipParameters(CompressionMethod compressionMethod, boolean encrypt,
                                         EncryptionMethod encryptionMethod, AesKeyStrength aesKeyStrength) {
  ZipParameters zipParameters = new ZipParameters();
  zipParameters.setCompressionMethod(compressionMethod);
  zipParameters.setEncryptionMethod(encryptionMethod);
  zipParameters.setAesKeyStrength(aesKeyStrength);
  zipParameters.setEncryptFiles(encrypt);
  return zipParameters;
}
 
Example 12
Source File: AbstractIT.java    From zip4j with Apache License 2.0 5 votes vote down vote up
protected ZipParameters createZipParameters(EncryptionMethod encryptionMethod, AesKeyStrength aesKeyStrength) {
  ZipParameters zipParameters = new ZipParameters();
  zipParameters.setEncryptFiles(true);
  zipParameters.setEncryptionMethod(encryptionMethod);
  zipParameters.setAesKeyStrength(aesKeyStrength);
  return zipParameters;
}
 
Example 13
Source File: FileHeaderFactoryTest.java    From zip4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testGenerateFileHeaderWithAesEncryptionWithNullVersionUsesV2() throws ZipException {
  ZipParameters zipParameters = generateZipParameters();
  zipParameters.setEncryptFiles(true);
  zipParameters.setEncryptionMethod(EncryptionMethod.AES);
  zipParameters.setAesKeyStrength(AesKeyStrength.KEY_STRENGTH_256);
  zipParameters.setAesVersion(null);

  FileHeader fileHeader = fileHeaderFactory.generateFileHeader(zipParameters, false, 0, InternalZipConstants.CHARSET_UTF_8, rawIO);
  verifyFileHeader(fileHeader, zipParameters, false, 0, 51, true);
  verifyAesExtraDataRecord(fileHeader.getAesExtraDataRecord(), AesKeyStrength.KEY_STRENGTH_256,
      CompressionMethod.DEFLATE, AesVersion.TWO);
}
 
Example 14
Source File: FileHeaderFactoryTest.java    From zip4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testGenerateFileHeaderWithAesEncryptionVersionV1() throws ZipException {
  ZipParameters zipParameters = generateZipParameters();
  zipParameters.setEncryptFiles(true);
  zipParameters.setEncryptionMethod(EncryptionMethod.AES);
  zipParameters.setAesKeyStrength(AesKeyStrength.KEY_STRENGTH_256);
  zipParameters.setAesVersion(AesVersion.ONE);

  FileHeader fileHeader = fileHeaderFactory.generateFileHeader(zipParameters, false, 0, InternalZipConstants.CHARSET_UTF_8, rawIO);
  verifyFileHeader(fileHeader, zipParameters, false, 0, 51, true);
  verifyAesExtraDataRecord(fileHeader.getAesExtraDataRecord(), AesKeyStrength.KEY_STRENGTH_256,
      CompressionMethod.DEFLATE, AesVersion.ONE);
}
 
Example 15
Source File: FileHeaderFactoryTest.java    From zip4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testGenerateFileHeaderWithAesEncryptionWithKeyStrength256() throws ZipException {
  ZipParameters zipParameters = generateZipParameters();
  zipParameters.setEncryptFiles(true);
  zipParameters.setEncryptionMethod(EncryptionMethod.AES);
  zipParameters.setAesKeyStrength(AesKeyStrength.KEY_STRENGTH_256);

  FileHeader fileHeader = fileHeaderFactory.generateFileHeader(zipParameters, false, 0, InternalZipConstants.CHARSET_UTF_8, rawIO);
  verifyFileHeader(fileHeader, zipParameters, false, 0, 51, true);
  verifyAesExtraDataRecord(fileHeader.getAesExtraDataRecord(), AesKeyStrength.KEY_STRENGTH_256,
      CompressionMethod.DEFLATE, AesVersion.TWO);
}
 
Example 16
Source File: FileHeaderFactoryTest.java    From zip4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testGenerateFileHeaderWithAesEncryptionWithKeyStrength192() throws ZipException {
  ZipParameters zipParameters = generateZipParameters();
  zipParameters.setEncryptFiles(true);
  zipParameters.setEncryptionMethod(EncryptionMethod.AES);
  zipParameters.setAesKeyStrength(AesKeyStrength.KEY_STRENGTH_192);

  FileHeader fileHeader = fileHeaderFactory.generateFileHeader(zipParameters, false, 0, InternalZipConstants.CHARSET_UTF_8, rawIO);
  verifyFileHeader(fileHeader, zipParameters, false, 0, 51, true);
  verifyAesExtraDataRecord(fileHeader.getAesExtraDataRecord(), AesKeyStrength.KEY_STRENGTH_192,
      CompressionMethod.DEFLATE, AesVersion.TWO);
}
 
Example 17
Source File: FileHeaderFactoryTest.java    From zip4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testGenerateFileHeaderWithAesEncryptionWithKeyStrength128() throws ZipException {
  ZipParameters zipParameters = generateZipParameters();
  zipParameters.setEncryptFiles(true);
  zipParameters.setEncryptionMethod(EncryptionMethod.AES);
  zipParameters.setAesKeyStrength(AesKeyStrength.KEY_STRENGTH_128);

  FileHeader fileHeader = fileHeaderFactory.generateFileHeader(zipParameters, false, 0, InternalZipConstants.CHARSET_UTF_8, rawIO);
  verifyFileHeader(fileHeader, zipParameters, false, 0, 51, true);
  verifyAesExtraDataRecord(fileHeader.getAesExtraDataRecord(), AesKeyStrength.KEY_STRENGTH_128,
      CompressionMethod.DEFLATE, AesVersion.TWO);
}
 
Example 18
Source File: RNZipArchiveModule.java    From react-native-zip-archive with MIT License 4 votes vote down vote up
@ReactMethod
public void zipWithPassword(String fileOrDirectory, String destDirectory, String password,
    String encryptionMethod, Promise promise) {
  try{

    ZipParameters parameters = new ZipParameters();
    parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
    parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);

    String encParts[] = encryptionMethod.split("-");

    if (password != null && !password.isEmpty()) {
      parameters.setEncryptFiles(true);
      if (encParts[0].equals("AES")) {
        parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);
        if (encParts[1].equals("128")) {
          parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_128);
        } else if (encParts[1].equals("256")) {
          parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);
        } else {
          parameters.setAesKeyStrength(Zip4jConstants.ENC_METHOD_STANDARD);
        }
      } else if (encryptionMethod.equals("STANDARD")) {
        parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD);
        Log.d(TAG, "Standard Encryption");
      } else {
        parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD);
        Log.d(TAG, "Encryption type not supported default to Standard Encryption");
      }
      parameters.setPassword(password);
    } else {
      promise.reject(null, "Password is empty");
    }

    processZip(fileOrDirectory, destDirectory, parameters, promise);

  } catch (Exception ex) {
    promise.reject(null, ex.getMessage());
    return;
  }

}