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

The following examples show how to use net.lingala.zip4j.model.ZipParameters#setEncryptionMethod() . 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: AbstractAddFileToZipTask.java    From zip4j with Apache License 2.0 6 votes vote down vote up
void verifyZipParameters(ZipParameters parameters) throws ZipException {
  if (parameters == null) {
    throw new ZipException("cannot validate zip parameters");
  }

  if (parameters.getCompressionMethod() != STORE && parameters.getCompressionMethod() != DEFLATE) {
    throw new ZipException("unsupported compression type");
  }

  if (parameters.isEncryptFiles()) {
    if (parameters.getEncryptionMethod() == NONE) {
      throw new ZipException("Encryption method has to be set, when encrypt files flag is set");
    }

    if (password == null || password.length <= 0) {
      throw new ZipException("input password is empty or null");
    }
  } else {
    parameters.setEncryptionMethod(NONE);
  }
}
 
Example 2
Source File: MiscZipFileIT.java    From zip4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testCustomThreadFactory() throws IOException {
  TestUtils.copyFileToFolder(getTestFileFromResources("file_PDF_1MB.pdf"), temporaryFolder.getRoot(), 1000);

  String threadName = "CustomThreadFactoryTest";
  ZipFile zipFile = new ZipFile(generatedZipFile);
  zipFile.setThreadFactory(r -> {
    Thread t = new Thread(threadName);
    t.setDaemon(false);
    return t;
  });
  zipFile.setRunInThread(true);

  ZipParameters zipParameters = new ZipParameters();
  zipParameters.setEncryptFiles(true);
  zipParameters.setEncryptionMethod(EncryptionMethod.AES);

  zipFile.addFolder(temporaryFolder.getRoot(), zipParameters);

  Set<Thread> threadSet = Thread.getAllStackTraces().keySet();
  List<Thread> zip4jThread = threadSet.stream().filter(e -> e.getName().equals(threadName)).collect(Collectors.toList());
  assertThat(zip4jThread).hasSize(1);
  assertThat(zip4jThread.get(0).getName()).isEqualTo(threadName);
  assertThat(zip4jThread.get(0).isDaemon()).isFalse();
}
 
Example 3
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 4
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 5
Source File: AddFilesToZipIT.java    From zip4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testAddFilesWhenFilesAlreadyExistsRemovesFiles() throws IOException {
  ZipParameters zipParameters = createZipParameters(EncryptionMethod.AES, AesKeyStrength.KEY_STRENGTH_256);
  ZipFile zipFile = new ZipFile(generatedZipFile, PASSWORD);
  zipFile.addFiles(FILES_TO_ADD, zipParameters);

  char[] newPassword = "SOME_OTHER_PASSWORD".toCharArray();
  zipFile = new ZipFile(generatedZipFile, newPassword);
  zipParameters.setEncryptionMethod(EncryptionMethod.ZIP_STANDARD);

  zipFile.addFiles(FILES_TO_ADD, zipParameters);

  ZipFileVerifier.verifyZipFileByExtractingAllFiles(generatedZipFile, newPassword, outputFolder, FILES_TO_ADD.size());
  List<String> fileNames = FILES_TO_ADD.stream().map(File::getName).collect(Collectors.toList());
  verifyZipFileContainsFiles(generatedZipFile, fileNames, CompressionMethod.DEFLATE, EncryptionMethod.ZIP_STANDARD,
      null);
}
 
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: 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 8
Source File: ZipVersionUtilsTest.java    From zip4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testDetermineVersionNeededToExtractAES() {
  ZipParameters zipParameters = new ZipParameters();
  zipParameters.setEncryptFiles(true);
  zipParameters.setEncryptionMethod(EncryptionMethod.AES);
  assertThat(ZipVersionUtils.determineVersionNeededToExtract(zipParameters)).isEqualTo(VersionNeededToExtract.AES_ENCRYPTED);
}
 
Example 9
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 10
Source File: RenameFilesInZipIT.java    From zip4j with Apache License 2.0 5 votes vote down vote up
private ZipParameters buildZipParameters(CompressionMethod compressionMethod, boolean encrypt, EncryptionMethod encryptionMethod) {
  ZipParameters zipParameters = new ZipParameters();
  zipParameters.setEncryptFiles(encrypt);
  zipParameters.setEncryptionMethod(encryptionMethod);
  zipParameters.setCompressionMethod(compressionMethod);
  return zipParameters;
}
 
Example 11
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 12
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 13
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 14
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 15
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 16
Source File: FileHeaderFactoryTest.java    From zip4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testGenerateFileHeaderWithStandardEncryption() throws ZipException {
  ZipParameters zipParameters = generateZipParameters();
  zipParameters.setEncryptFiles(true);
  zipParameters.setEncryptionMethod(EncryptionMethod.ZIP_STANDARD);

  FileHeader fileHeader = fileHeaderFactory.generateFileHeader(zipParameters, false, 0, InternalZipConstants.CHARSET_UTF_8, rawIO);
  verifyFileHeader(fileHeader, zipParameters, false, 0, 20, false);
}
 
Example 17
Source File: AddFilesToZipIT.java    From zip4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testAddFilesWithDifferentEncryptionType() throws IOException {
  ZipParameters zipParameters = createZipParameters(EncryptionMethod.AES, AesKeyStrength.KEY_STRENGTH_256);
  ZipFile zipFile = new ZipFile(generatedZipFile, PASSWORD);
  zipFile.addFiles(singletonList(TestUtils.getTestFileFromResources("sample.pdf")), zipParameters);

  zipParameters.setEncryptionMethod(EncryptionMethod.ZIP_STANDARD);

  zipFile.addFiles(singletonList(TestUtils.getTestFileFromResources("file_PDF_1MB.pdf")), zipParameters);

  ZipFileVerifier.verifyZipFileByExtractingAllFiles(generatedZipFile, PASSWORD, outputFolder, 2);
}
 
Example 18
Source File: ExtractZipFileIT.java    From zip4j with Apache License 2.0 5 votes vote down vote up
private File createZipFileAndSplit(List<File> filesToAddToZip, long splitLength, boolean encrypt,
                                   EncryptionMethod encryptionMethod) throws IOException {

  ZipFile zipFile = new ZipFile(generatedZipFile, PASSWORD);
  ZipParameters zipParameters = new ZipParameters();
  zipParameters.setEncryptFiles(encrypt);
  zipParameters.setEncryptionMethod(encryptionMethod);
  zipFile.addFiles(filesToAddToZip, zipParameters);

  return TestUtils.splitFileWith7ZipFormat(zipFile.getFile(), temporaryFolder.getRoot(), splitLength);
}
 
Example 19
Source File: ExtractZipFileIT.java    From zip4j with Apache License 2.0 5 votes vote down vote up
private void addFileToZip(ZipFile zipFile, String fileName, EncryptionMethod encryptionMethod, String password) throws ZipException {
  ZipParameters zipParameters = new ZipParameters();
  zipParameters.setEncryptFiles(encryptionMethod != null);
  zipParameters.setEncryptionMethod(encryptionMethod);

  if (password != null) {
    zipFile.setPassword(password.toCharArray());
  }

  zipFile.addFile(getTestFileFromResources(fileName), zipParameters);
}
 
Example 20
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;
  }

}