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

The following examples show how to use net.lingala.zip4j.model.ZipParameters#setEncryptFiles() . 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: 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 2
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 3
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 4
Source File: AddFilesToZipIT.java    From zip4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testAddFileDoesNotOverrideFileIfFlagIsDisabled() throws IOException {
  ZipFile zipFile = new ZipFile(generatedZipFile);
  zipFile.addFiles(FILES_TO_ADD);

  ZipParameters zipParameters = new ZipParameters();
  zipParameters.setCompressionMethod(CompressionMethod.STORE);
  zipParameters.setEncryptFiles(true);
  zipParameters.setEncryptionMethod(EncryptionMethod.AES);
  zipParameters.setOverrideExistingFilesInZip(false);
  zipFile.setPassword(PASSWORD);
  zipFile.addFile(TestUtils.getTestFileFromResources("sample_text_large.txt"), zipParameters);

  ZipFileVerifier.verifyZipFileByExtractingAllFiles(generatedZipFile, outputFolder, 3);
  verifyZipFileContainsFiles(generatedZipFile, singletonList("sample_text_large.txt"), CompressionMethod.DEFLATE,
      null, null);
}
 
Example 5
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 6
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 7
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 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: RemoveFilesFromZipIT.java    From zip4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testRemoveFilesRemovesMultipleEntriesFromEncryptedZipSuccessfully() throws IOException {
  ZipFile zipFile = new ZipFile(generatedZipFile, PASSWORD);
  ZipParameters zipParameters = new ZipParameters();
  zipParameters.setEncryptFiles(true);
  zipParameters.setEncryptionMethod(EncryptionMethod.AES);
  zipFile.addFiles(FILES_TO_ADD, zipParameters);
  List<String> filesToRemove = Arrays.asList("sample_text1.txt", "sample.pdf");

  zipFile.removeFiles(filesToRemove);

  ZipFileVerifier.verifyZipFileByExtractingAllFiles(generatedZipFile, PASSWORD, outputFolder, FILES_TO_ADD.size() - 2);
  verifyFileHeadersDoesNotExist(zipFile, filesToRemove);
}
 
Example 10
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 11
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 12
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 13
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 14
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 15
Source File: FileHeaderFactoryTest.java    From zip4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testGenerateFileHeaderWhenEncryptingWithoutMethodThrowsException() throws ZipException {
  expectedException.expect(ZipException.class);
  expectedException.expectMessage("Encryption method has to be set when encryptFiles flag is set in zip parameters");

  ZipParameters zipParameters = generateZipParameters();
  zipParameters.setEncryptFiles(true);

  fileHeaderFactory.generateFileHeader(zipParameters, false, 0, InternalZipConstants.CHARSET_UTF_8, rawIO);
}
 
Example 16
Source File: ExtractZipFileIT.java    From zip4j with Apache License 2.0 5 votes vote down vote up
private ZipParameters createZipParametersForNestedZip(EncryptionMethod encryptionMethod) {
  ZipParameters zipParameters = new ZipParameters();
  if (encryptionMethod != null && !encryptionMethod.equals(EncryptionMethod.NONE)) {
    zipParameters.setEncryptFiles(true);
    zipParameters.setEncryptionMethod(encryptionMethod);
  }
  return zipParameters;
}
 
Example 17
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 18
Source File: AbstractAddFileToZipTask.java    From zip4j with Apache License 2.0 5 votes vote down vote up
private ZipParameters cloneAndAdjustZipParameters(ZipParameters zipParameters, File fileToAdd,
                                                  ProgressMonitor progressMonitor) throws IOException {
  ZipParameters clonedZipParameters = new ZipParameters(zipParameters);
  clonedZipParameters.setLastModifiedFileTime(javaToDosTime((fileToAdd.lastModified())));

  if (fileToAdd.isDirectory()) {
    clonedZipParameters.setEntrySize(0);
  } else {
    clonedZipParameters.setEntrySize(fileToAdd.length());
  }

  clonedZipParameters.setWriteExtendedLocalFileHeader(false);
  clonedZipParameters.setLastModifiedFileTime(fileToAdd.lastModified());

  if (!Zip4jUtil.isStringNotNullAndNotEmpty(zipParameters.getFileNameInZip())) {
    String relativeFileName = getRelativeFileName(fileToAdd, zipParameters);
    clonedZipParameters.setFileNameInZip(relativeFileName);
  }

  if (fileToAdd.isDirectory()) {
    clonedZipParameters.setCompressionMethod(CompressionMethod.STORE);
    clonedZipParameters.setEncryptionMethod(EncryptionMethod.NONE);
    clonedZipParameters.setEncryptFiles(false);
  } else {
    if (clonedZipParameters.isEncryptFiles() && clonedZipParameters.getEncryptionMethod() == ZIP_STANDARD) {
      progressMonitor.setCurrentTask(CALCULATE_CRC);
      clonedZipParameters.setEntryCRC(computeFileCrc(fileToAdd, progressMonitor));
      progressMonitor.setCurrentTask(ADD_ENTRY);
    }

    if (fileToAdd.length() == 0) {
      clonedZipParameters.setCompressionMethod(CompressionMethod.STORE);
    }
  }

  return clonedZipParameters;
}
 
Example 19
Source File: AbstractAddFileToZipTask.java    From zip4j with Apache License 2.0 5 votes vote down vote up
private void addSymlinkToZip(File fileToAdd, ZipOutputStream zipOutputStream, ZipParameters zipParameters,
                             SplitOutputStream splitOutputStream) throws IOException {

  ZipParameters clonedZipParameters = new ZipParameters(zipParameters);
  clonedZipParameters.setFileNameInZip(replaceFileNameInZip(zipParameters.getFileNameInZip(), fileToAdd.getName()));
  clonedZipParameters.setEncryptFiles(false);
  clonedZipParameters.setCompressionMethod(CompressionMethod.STORE);

  zipOutputStream.putNextEntry(clonedZipParameters);

  String symLinkTarget = FileUtils.readSymbolicLink(fileToAdd);
  zipOutputStream.write(symLinkTarget.getBytes());

  closeEntry(zipOutputStream, splitOutputStream, fileToAdd, true);
}
 
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;
  }

}