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

The following examples show how to use net.lingala.zip4j.model.ZipParameters#getEncryptionMethod() . 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
long calculateWorkForFiles(List<File> filesToAdd, ZipParameters zipParameters) throws ZipException {
  long totalWork = 0;

  for (File fileToAdd : filesToAdd) {
    if (!fileToAdd.exists()) {
      continue;
    }

    if (zipParameters.isEncryptFiles() && zipParameters.getEncryptionMethod() == EncryptionMethod.ZIP_STANDARD) {
      totalWork += (fileToAdd.length() * 2); // for CRC calculation
    } else {
      totalWork += fileToAdd.length();
    }

    //If an entry already exists, we have to remove that entry first and then add content again.
    //In this case, add corresponding work
    String relativeFileName = getRelativeFileName(fileToAdd, zipParameters);
    FileHeader fileHeader = getFileHeader(getZipModel(), relativeFileName);
    if (fileHeader != null) {
      totalWork += (getZipModel().getZipFile().length() - fileHeader.getCompressedSize());
    }
  }

  return totalWork;
}
 
Example 2
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 3
Source File: ZipOutputStream.java    From zip4j with Apache License 2.0 6 votes vote down vote up
private CipherOutputStream initializeCipherOutputStream(ZipEntryOutputStream zipEntryOutputStream,
                                                        ZipParameters zipParameters) throws IOException {
  if (!zipParameters.isEncryptFiles()) {
    return new NoCipherOutputStream(zipEntryOutputStream, zipParameters, null);
  }

  if (password == null || password.length == 0) {
    throw new ZipException("password not set");
  }

  if (zipParameters.getEncryptionMethod() == EncryptionMethod.AES) {
    return new AesCipherOutputStream(zipEntryOutputStream, zipParameters, password);
  } else if (zipParameters.getEncryptionMethod() == EncryptionMethod.ZIP_STANDARD) {
    return new ZipStandardCipherOutputStream(zipEntryOutputStream, zipParameters, password);
  } else {
    throw new ZipException("Invalid encryption method");
  }
}
 
Example 4
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 5
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 6
Source File: ZipEngine.java    From AndroidZip with Apache License 2.0 5 votes vote down vote up
private long calculateTotalWork(ArrayList fileList, ZipParameters parameters) throws ZipException {
	if (fileList == null) {
		throw new ZipException("file list is null, cannot calculate total work");
	}
	
	long totalWork = 0;
	
	for (int i = 0; i < fileList.size(); i++) {
		if(fileList.get(i) instanceof File) {
			if (((File)fileList.get(i)).exists()) {
				if (parameters.isEncryptFiles() && 
						parameters.getEncryptionMethod() == Zip4jConstants.ENC_METHOD_STANDARD) {
					totalWork += (Zip4jUtil.getFileLengh((File)fileList.get(i)) * 2);
				} else {
					totalWork += Zip4jUtil.getFileLengh((File)fileList.get(i));
				}
				
				if (zipModel.getCentralDirectory() != null && 
						zipModel.getCentralDirectory().getFileHeaders() != null && 
						zipModel.getCentralDirectory().getFileHeaders().size() > 0) {
					String relativeFileName = Zip4jUtil.getRelativeFileName(
							((File)fileList.get(i)).getAbsolutePath(), parameters.getRootFolderInZip(), parameters.getDefaultFolderPath());
					FileHeader fileHeader = Zip4jUtil.getFileHeader(zipModel, relativeFileName);
					if (fileHeader != null) {
						totalWork += (Zip4jUtil.getFileLengh(new File(zipModel.getZipFile())) - fileHeader.getCompressedSize());
					}
				}
			}
		}
	}
	
	return totalWork;
}
 
Example 7
Source File: FileHeaderFactory.java    From zip4j with Apache License 2.0 4 votes vote down vote up
public FileHeader generateFileHeader(ZipParameters zipParameters, boolean isSplitZip, int currentDiskNumberStart,
                                     Charset charset, RawIO rawIO)
    throws ZipException {

  FileHeader fileHeader = new FileHeader();
  fileHeader.setSignature(HeaderSignature.CENTRAL_DIRECTORY);
  fileHeader.setVersionMadeBy(determineVersionMadeBy(zipParameters, rawIO));
  fileHeader.setVersionNeededToExtract(determineVersionNeededToExtract(zipParameters).getCode());

  if (zipParameters.isEncryptFiles() && zipParameters.getEncryptionMethod() == EncryptionMethod.AES) {
    fileHeader.setCompressionMethod(CompressionMethod.AES_INTERNAL_ONLY);
    fileHeader.setAesExtraDataRecord(generateAESExtraDataRecord(zipParameters));
    fileHeader.setExtraFieldLength(fileHeader.getExtraFieldLength() + InternalZipConstants.AES_EXTRA_DATA_RECORD_SIZE);
  } else {
    fileHeader.setCompressionMethod(zipParameters.getCompressionMethod());
  }

  if (zipParameters.isEncryptFiles()) {
    if (zipParameters.getEncryptionMethod() == null || zipParameters.getEncryptionMethod() == EncryptionMethod.NONE) {
      throw new ZipException("Encryption method has to be set when encryptFiles flag is set in zip parameters");
    }

    fileHeader.setEncrypted(true);
    fileHeader.setEncryptionMethod(zipParameters.getEncryptionMethod());
  }

  String fileName = validateAndGetFileName(zipParameters.getFileNameInZip());
  fileHeader.setFileName(fileName);
  fileHeader.setFileNameLength(determineFileNameLength(fileName, charset));
  fileHeader.setDiskNumberStart(isSplitZip ? currentDiskNumberStart : 0);

  if (zipParameters.getLastModifiedFileTime() > 0) {
    fileHeader.setLastModifiedTime(Zip4jUtil.javaToDosTime(zipParameters.getLastModifiedFileTime()));
  } else {
    fileHeader.setLastModifiedTime(Zip4jUtil.javaToDosTime(System.currentTimeMillis()));
  }

  //For files added by this library, this attribute will be set after closeEntry is done
  fileHeader.setExternalFileAttributes(new byte[4]);
  fileHeader.setDirectory(isZipEntryDirectory(fileName));

  if (zipParameters.isWriteExtendedLocalFileHeader() && zipParameters.getEntrySize() == -1) {
    fileHeader.setUncompressedSize(0);
  } else {
    fileHeader.setUncompressedSize(zipParameters.getEntrySize());
  }

  if (zipParameters.isEncryptFiles() && zipParameters.getEncryptionMethod() == EncryptionMethod.ZIP_STANDARD) {
    fileHeader.setCrc(zipParameters.getEntryCRC());
  }

  fileHeader.setGeneralPurposeFlag(determineGeneralPurposeBitFlag(fileHeader.isEncrypted(), zipParameters, charset));
  fileHeader.setDataDescriptorExists(zipParameters.isWriteExtendedLocalFileHeader());
  fileHeader.setFileComment(zipParameters.getFileComment());
  return fileHeader;
}