net.lingala.zip4j.model.enums.CompressionLevel Java Examples

The following examples show how to use net.lingala.zip4j.model.enums.CompressionLevel. 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: FileHeaderFactoryTest.java    From zip4j with Apache License 2.0 6 votes vote down vote up
private void verifyCompressionLevelGridForDeflate(CompressionLevel compressionLevel,
                                                  byte firstByteOfGeneralPurposeBytes) {
  if (CompressionLevel.NORMAL.equals(compressionLevel)) {
    assertThat(isBitSet(firstByteOfGeneralPurposeBytes, 1)).isFalse();
    assertThat(isBitSet(firstByteOfGeneralPurposeBytes, 2)).isFalse();
  } else if (CompressionLevel.MAXIMUM.equals(compressionLevel)) {
    assertThat(isBitSet(firstByteOfGeneralPurposeBytes, 1)).isTrue();
    assertThat(isBitSet(firstByteOfGeneralPurposeBytes, 2)).isFalse();
  } else if (CompressionLevel.FAST.equals(compressionLevel)) {
    assertThat(isBitSet(firstByteOfGeneralPurposeBytes, 1)).isFalse();
    assertThat(isBitSet(firstByteOfGeneralPurposeBytes, 2)).isTrue();
  } else if (CompressionLevel.FASTEST.equals(compressionLevel)
      || CompressionLevel.ULTRA.equals(compressionLevel)) {
    assertThat(isBitSet(firstByteOfGeneralPurposeBytes, 1)).isTrue();
    assertThat(isBitSet(firstByteOfGeneralPurposeBytes, 2)).isTrue();
  } else {
    throw new RuntimeException("Invalid compression level");
  }
}
 
Example #2
Source File: ZipFileUtil.java    From java-trader with Apache License 2.0 6 votes vote down vote up
public static void archiveAdd(File zip, File toAdd, String pathInZip)
        throws IOException
{
    if ( pathInZip==null ) {
        pathInZip = toAdd.getName();
    }

    net.lingala.zip4j.ZipFile zipFile = new net.lingala.zip4j.ZipFile(zip);

    List<FileHeader> items = zipFile.getFileHeaders();
    for(FileHeader zipItem : items) {
        if ( zipItem.getFileName().equalsIgnoreCase(pathInZip)) {
            zipFile.removeFile(zipItem);
        }
    }
    net.lingala.zip4j.model.ZipParameters zipParams = new net.lingala.zip4j.model.ZipParameters();
    zipParams.setCompressionLevel(CompressionLevel.MAXIMUM);
    zipParams.setFileNameInZip(pathInZip);
    zipFile.addFile(toAdd, zipParams);

}
 
Example #3
Source File: ZipFileUtil.java    From java-trader with Apache License 2.0 6 votes vote down vote up
public static void archiveAddAll(File zip, final List<String> pathInZips, final List<byte[]> datas)
        throws IOException
{
    net.lingala.zip4j.ZipFile zipFile = new net.lingala.zip4j.ZipFile(zip);

    List<FileHeader> items = new ArrayList<>( zipFile.getFileHeaders() );

    for(FileHeader zipItem : items) {
        if ( pathInZips.contains(zipItem.getFileName()) ) {
            zipFile.removeFile(zipItem);
        }
    }

    for(int i=0;i<pathInZips.size();i++) {
        net.lingala.zip4j.model.ZipParameters zipParams = new net.lingala.zip4j.model.ZipParameters();
        byte[] data = datas.get(i);
        zipParams.setCompressionLevel(CompressionLevel.MAXIMUM);
        zipParams.setFileNameInZip(pathInZips.get(i));
        zipFile.addStream(new ByteArrayInputStream(data), zipParams);
    }
}
 
Example #4
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 #5
Source File: FileHeaderFactory.java    From zip4j with Apache License 2.0 5 votes vote down vote up
private byte generateFirstGeneralPurposeByte(boolean isEncrypted, ZipParameters zipParameters) {

    byte firstByte = 0;

    if (isEncrypted) {
      firstByte = setBit(firstByte, 0);
    }

    if (CompressionMethod.DEFLATE.equals(zipParameters.getCompressionMethod())) {
      if (CompressionLevel.NORMAL.equals(zipParameters.getCompressionLevel())) {
        firstByte = unsetBit(firstByte, 1);
        firstByte = unsetBit(firstByte, 2);
      } else if (CompressionLevel.MAXIMUM.equals(zipParameters.getCompressionLevel())) {
        firstByte = setBit(firstByte, 1);
        firstByte = unsetBit(firstByte, 2);
      } else if (CompressionLevel.FAST.equals(zipParameters.getCompressionLevel())) {
        firstByte = unsetBit(firstByte, 1);
        firstByte = setBit(firstByte, 2);
      } else if (CompressionLevel.FASTEST.equals(zipParameters.getCompressionLevel())
          || CompressionLevel.ULTRA.equals(zipParameters.getCompressionLevel())) {
        firstByte = setBit(firstByte, 1);
        firstByte = setBit(firstByte, 2);
      }
    }

    if (zipParameters.isWriteExtendedLocalFileHeader()) {
      firstByte = setBit(firstByte, 3);
    }

    return firstByte;
  }
 
Example #6
Source File: FileHeaderFactoryTest.java    From zip4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testGenerateFileHeaderDefaults() throws ZipException {
  FileHeader fileHeader = fileHeaderFactory.generateFileHeader(generateZipParameters(), false, 0, InternalZipConstants.CHARSET_UTF_8, rawIO);

  assertThat(fileHeader).isNotNull();
  assertThat(fileHeader.getCompressionMethod()).isEqualTo(CompressionMethod.DEFLATE);
  verifyCompressionLevelGridForDeflate(CompressionLevel.NORMAL,
      fileHeader.getGeneralPurposeFlag()[0]);
  assertThat(fileHeader.isEncrypted()).isFalse();
  assertThat(fileHeader.getEncryptionMethod()).isEqualTo(EncryptionMethod.NONE);
  assertThat(fileHeader.getAesExtraDataRecord()).isNull();
  assertThat(fileHeader.getLastModifiedTime()).isNotZero();
  assertThat(fileHeader.getCompressedSize()).isEqualTo(0);
  assertThat(fileHeader.getUncompressedSize()).isEqualTo(0);
}
 
Example #7
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 #8
Source File: FileHeaderFactoryTest.java    From zip4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testGenerateFileHeaderWithCompressionLevelMaximum() throws ZipException {
  ZipParameters zipParameters = generateZipParameters();
  zipParameters.setCompressionLevel(CompressionLevel.MAXIMUM);

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

  verifyCompressionLevelGridForDeflate(CompressionLevel.MAXIMUM, fileHeader.getGeneralPurposeFlag()[0]);
}
 
Example #9
Source File: FileHeaderFactoryTest.java    From zip4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testGenerateFileHeaderWithCompressionLevelFast() throws ZipException {
  ZipParameters zipParameters = generateZipParameters();
  zipParameters.setCompressionLevel(CompressionLevel.FAST);

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

  verifyCompressionLevelGridForDeflate(CompressionLevel.FAST, fileHeader.getGeneralPurposeFlag()[0]);
}
 
Example #10
Source File: FileHeaderFactoryTest.java    From zip4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testGenerateFileHeaderWithCompressionLevelFastest() throws ZipException {
  ZipParameters zipParameters = generateZipParameters();
  zipParameters.setCompressionLevel(CompressionLevel.FASTEST);

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

  verifyCompressionLevelGridForDeflate(CompressionLevel.FASTEST, fileHeader.getGeneralPurposeFlag()[0]);
}
 
Example #11
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 #12
Source File: DeflaterOutputStream.java    From zip4j with Apache License 2.0 4 votes vote down vote up
public DeflaterOutputStream(CipherOutputStream cipherOutputStream, CompressionLevel compressionLevel) {
  super(cipherOutputStream);
  deflater = new Deflater(compressionLevel.getLevel(), true);
}
 
Example #13
Source File: ZipParameters.java    From zip4j with Apache License 2.0 2 votes vote down vote up
/**
 * Get the compression level used to compress files
 * @return the compression level used to compress files
 */
public CompressionLevel getCompressionLevel() {
  return compressionLevel;
}
 
Example #14
Source File: ZipParameters.java    From zip4j with Apache License 2.0 2 votes vote down vote up
/**
 * Set the compression level used to compress files
 * @param compressionLevel the compression level used to compress files
 */
public void setCompressionLevel(CompressionLevel compressionLevel) {
  this.compressionLevel = compressionLevel;
}