net.lingala.zip4j.model.LocalFileHeader Java Examples

The following examples show how to use net.lingala.zip4j.model.LocalFileHeader. 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: AuthenticatorPlusImporter.java    From Aegis with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void decrypt(Context context, DecryptListener listener) {
    Dialogs.showPasswordInputDialog(context, password -> {
        try (ByteArrayInputStream inStream = new ByteArrayInputStream(_data);
             ZipInputStream zipStream = new ZipInputStream(inStream, password)) {
            LocalFileHeader header;
            while ((header = zipStream.getNextEntry()) != null) {
                File file = new File(header.getFileName());
                if (file.getName().equals(FILENAME)) {
                    FileReader reader = new FileReader(zipStream);
                    GoogleAuthUriImporter importer = new GoogleAuthUriImporter(context);
                    GoogleAuthUriImporter.State state = importer.read(reader);
                    listener.onStateDecrypted(state);
                    return;
                }
            }

            throw new FileNotFoundException(FILENAME);
        } catch (IOException | DatabaseImporterException e) {
            listener.onError(e);
        }
    });
}
 
Example #2
Source File: ZipInputStream.java    From zip4j with Apache License 2.0 6 votes vote down vote up
public LocalFileHeader getNextEntry(FileHeader fileHeader) throws IOException {
  if (localFileHeader != null) {
    readUntilEndOfEntry();
  }

  localFileHeader = headerReader.readLocalFileHeader(inputStream, charset);

  if (localFileHeader == null) {
    return null;
  }

  verifyLocalFileHeader(localFileHeader);
  crc32.reset();

  if (fileHeader != null) {
    localFileHeader.setCrc(fileHeader.getCrc());
    localFileHeader.setCompressedSize(fileHeader.getCompressedSize());
    localFileHeader.setUncompressedSize(fileHeader.getUncompressedSize());
    canSkipExtendedLocalFileHeader = true;
  } else {
    canSkipExtendedLocalFileHeader = false;
  }

  this.decompressedInputStream = initializeEntryInputStream(localFileHeader);
  return localFileHeader;
}
 
Example #3
Source File: HeaderReader.java    From zip4j with Apache License 2.0 6 votes vote down vote up
private void readZip64ExtendedInfo(LocalFileHeader localFileHeader, RawIO rawIO) throws ZipException {
  if (localFileHeader == null) {
    throw new ZipException("file header is null in reading Zip64 Extended Info");
  }

  if (localFileHeader.getExtraDataRecords() == null || localFileHeader.getExtraDataRecords().size() <= 0) {
    return;
  }

  Zip64ExtendedInfo zip64ExtendedInfo = readZip64ExtendedInfo(localFileHeader.getExtraDataRecords(), rawIO,
      localFileHeader.getUncompressedSize(), localFileHeader.getCompressedSize(), 0, 0);

  if (zip64ExtendedInfo == null) {
    return;
  }

  localFileHeader.setZip64ExtendedInfo(zip64ExtendedInfo);

  if (zip64ExtendedInfo.getUncompressedSize() != -1) {
    localFileHeader.setUncompressedSize(zip64ExtendedInfo.getUncompressedSize());
  }

  if (zip64ExtendedInfo.getCompressedSize() != -1) {
    localFileHeader.setCompressedSize(zip64ExtendedInfo.getCompressedSize());
  }
}
 
Example #4
Source File: HeaderWriterIT.java    From zip4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testWriteLocalFileHeaderForZip64Format() throws IOException {
  ZipModel zipModel = createZipModel(10);
  LocalFileHeader localFileHeaderToWrite = createLocalFileHeader("LFH", COMPRESSED_SIZE_ZIP64,
      UNCOMPRESSED_SIZE_ZIP64, true);
  File headersFile = temporaryFolder.newFile();

  try(OutputStream outputStream = new FileOutputStream(headersFile)) {
    headerWriter.writeLocalFileHeader(zipModel, localFileHeaderToWrite, outputStream, InternalZipConstants.CHARSET_UTF_8);
  }

  try(InputStream inputStream = new FileInputStream(headersFile)) {
    LocalFileHeader readLocalFileHeader = headerReader.readLocalFileHeader(inputStream, InternalZipConstants.CHARSET_UTF_8);
    verifyLocalFileHeader(readLocalFileHeader, FILE_NAME_PREFIX + "LFH", COMPRESSED_SIZE_ZIP64,
        UNCOMPRESSED_SIZE_ZIP64);
    verifyZip64ExtendedInfo(readLocalFileHeader.getZip64ExtendedInfo(), COMPRESSED_SIZE_ZIP64,
        UNCOMPRESSED_SIZE_ZIP64, -1, -1);
  }

  verifyEntrySizesIsMaxValueInLFHWhenZip64Format(headersFile);
}
 
Example #5
Source File: HeaderWriterIT.java    From zip4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testUpdateLocalFileHeaderForZip64() throws IOException {
  File headersFile = temporaryFolder.newFile();
  createAndUpdateLocalFileHeader(headersFile, COMPRESSED_SIZE_ZIP64, UNCOMPRESSED_SIZE_ZIP64, 546423);

  try (InputStream inputStream = new FileInputStream(headersFile)) {
    LocalFileHeader readLocalFileHeader = headerReader.readLocalFileHeader(inputStream, InternalZipConstants.CHARSET_UTF_8);
    assertThat(readLocalFileHeader.getCompressedSize()).isEqualTo(COMPRESSED_SIZE_ZIP64 + 100);
    assertThat(readLocalFileHeader.getUncompressedSize()).isEqualTo(UNCOMPRESSED_SIZE_ZIP64 + 100);
    assertThat(readLocalFileHeader.getCrc()).isEqualTo(546423);
    verifyZip64ExtendedInfo(readLocalFileHeader.getZip64ExtendedInfo(), COMPRESSED_SIZE_ZIP64 + 100,
        UNCOMPRESSED_SIZE_ZIP64 + 100, -1, -1);
  }

  verifyEntrySizesIsMaxValueInLFHWhenZip64Format(headersFile);
}
 
Example #6
Source File: HeaderWriterIT.java    From zip4j with Apache License 2.0 6 votes vote down vote up
private void createAndUpdateLocalFileHeader(File headersFile, long compressedSize, long uncompressedSize, long crc)
    throws IOException {
  ZipModel zipModel = createZipModel(3);
  LocalFileHeader localFileHeaderToWrite = createLocalFileHeader("LFH", compressedSize, uncompressedSize, false);
  localFileHeaderToWrite.setCompressedSize(compressedSize);
  localFileHeaderToWrite.setUncompressedSize(uncompressedSize);
  localFileHeaderToWrite.setCrc(10);

  try (OutputStream outputStream = new FileOutputStream(headersFile)) {
    headerWriter.writeLocalFileHeader(zipModel, localFileHeaderToWrite, outputStream, InternalZipConstants.CHARSET_UTF_8);
  }

  try (SplitOutputStream splitOutputStream = new SplitOutputStream(headersFile)) {
    FileHeader fileHeader = createFileHeaders(1, compressedSize + 100, uncompressedSize + 100).get(0);
    fileHeader.setFileName(localFileHeaderToWrite.getFileName());
    fileHeader.setFileNameLength(fileHeader.getFileName().getBytes(InternalZipConstants.CHARSET_UTF_8).length);
    fileHeader.setCrc(crc);
    headerWriter.updateLocalFileHeader(fileHeader, zipModel, splitOutputStream);
  }
}
 
Example #7
Source File: HeaderWriterIT.java    From zip4j with Apache License 2.0 6 votes vote down vote up
private void testWriteExtendedLocalFileHeader(long compressedSize, long uncompressedSize, long crc,
                                              boolean isZip64Format) throws IOException {
  LocalFileHeader localFileHeader = createLocalFileHeader("SOME_NAME", compressedSize, uncompressedSize, true);
  localFileHeader.setCrc(crc);
  localFileHeader.setWriteCompressedSizeInZip64ExtraRecord(isZip64Format);
  File headersFile = temporaryFolder.newFile();

  try(OutputStream outputStream = new FileOutputStream(headersFile)) {
    headerWriter.writeExtendedLocalHeader(localFileHeader, outputStream);
  }

  try(InputStream inputStream = new FileInputStream(headersFile)) {
    DataDescriptor dataDescriptor = headerReader.readDataDescriptor(inputStream, isZip64Format);
    verifyDataDescriptor(dataDescriptor, compressedSize, uncompressedSize, crc);
  }
}
 
Example #8
Source File: HeaderWriterIT.java    From zip4j with Apache License 2.0 6 votes vote down vote up
private void testWriteLocalFileHeaderWithAes(AesKeyStrength aesKeyStrength, AesVersion aesVersion)
    throws IOException {
  ZipModel zipModel = createZipModel(10);
  LocalFileHeader localFileHeaderToWrite = createLocalFileHeader("TEXT", COMPRESSED_SIZE, UNCOMPRESSED_SIZE, true);
  localFileHeaderToWrite.setEncryptionMethod(EncryptionMethod.AES);
  localFileHeaderToWrite.setAesExtraDataRecord(createAesExtraDataRecord(aesKeyStrength, aesVersion));
  File headersFile = temporaryFolder.newFile();

  try(OutputStream outputStream = new FileOutputStream(headersFile)) {
    headerWriter.writeLocalFileHeader(zipModel, localFileHeaderToWrite, outputStream, InternalZipConstants.CHARSET_UTF_8);
  }

  try(InputStream inputStream = new FileInputStream(headersFile)) {
    LocalFileHeader readLocalFileHeader = headerReader.readLocalFileHeader(inputStream, InternalZipConstants.CHARSET_UTF_8);
    assertThat(readLocalFileHeader.getEncryptionMethod()).isEqualTo(EncryptionMethod.AES);
    verifyAesExtraDataRecord(readLocalFileHeader.getAesExtraDataRecord(), aesKeyStrength, aesVersion);
  }
}
 
Example #9
Source File: HeaderWriterIT.java    From zip4j with Apache License 2.0 6 votes vote down vote up
private void testWriteLocalFileHeaderWithFileNameAndCharset(String fileNameSuffix, boolean useUtf8,
                                                  boolean expectFileNamesToMatch, Charset charset) throws IOException {
  ZipModel zipModel = createZipModel(10);
  LocalFileHeader localFileHeaderToWrite = createLocalFileHeader(fileNameSuffix, COMPRESSED_SIZE, UNCOMPRESSED_SIZE,
      useUtf8);
  File headersFile = temporaryFolder.newFile();

  try(OutputStream outputStream = new FileOutputStream(headersFile)) {
    headerWriter.writeLocalFileHeader(zipModel, localFileHeaderToWrite, outputStream, charset);
  }

  try(InputStream inputStream = new FileInputStream(headersFile)) {
    LocalFileHeader readLocalFileHeader = headerReader.readLocalFileHeader(inputStream, charset);
    if (expectFileNamesToMatch) {
      assertThat(readLocalFileHeader.getFileName()).isEqualTo(FILE_NAME_PREFIX + fileNameSuffix);
    } else {
      assertThat(readLocalFileHeader.getFileName()).isNotEqualTo(FILE_NAME_PREFIX + fileNameSuffix);
    }
  }
}
 
Example #10
Source File: HeaderReaderIT.java    From zip4j with Apache License 2.0 6 votes vote down vote up
private File generateAndWriteLocalFileHeader(long entrySize, EncryptionMethod encryptionMethod)
    throws IOException {
  LocalFileHeader localFileHeader = generateLocalFileHeader(entrySize, encryptionMethod);

  if (encryptionMethod != null && encryptionMethod != EncryptionMethod.NONE) {
    localFileHeader.setEncrypted(true);
    localFileHeader.setEncryptionMethod(encryptionMethod);
  }

  ZipModel zipModel = generateZipModel(1);
  File headerFile = temporaryFolder.newFile();

  try(OutputStream outputStream = new FileOutputStream(headerFile)) {
    headerWriter.writeLocalFileHeader(zipModel, localFileHeader, outputStream, InternalZipConstants.CHARSET_UTF_8);
  }

  return headerFile;
}
 
Example #11
Source File: ZipInputStreamIT.java    From zip4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testExtractWithRandomLengthWithAesAndDeflateCompression() throws IOException {
  Random random = new Random();
  File createZipFile = createZipFile(CompressionMethod.DEFLATE, true, EncryptionMethod.AES, AesKeyStrength.KEY_STRENGTH_256, PASSWORD);
  LocalFileHeader localFileHeader;
  int readLen;
  byte[] readBuffer = new byte[4096];
  int numberOfEntriesExtracted = 0;

  try (FileInputStream fileInputStream = new FileInputStream(createZipFile)) {
    try (ZipInputStream zipInputStream = new ZipInputStream(fileInputStream, PASSWORD)) {
      while ((localFileHeader = zipInputStream.getNextEntry()) != null) {
        File extractedFile = temporaryFolder.newFile(localFileHeader.getFileName());
        try (OutputStream outputStream = new FileOutputStream(extractedFile)) {
          while ((readLen = zipInputStream.read(readBuffer, 0, random.nextInt((25 - 1) + 1) + 1)) != -1) {
            outputStream.write(readBuffer, 0, readLen);
          }
        }
        verifyFileContent(getTestFileFromResources(localFileHeader.getFileName()), extractedFile);
        numberOfEntriesExtracted++;
      }
    }
  }

  assertThat(numberOfEntriesExtracted).isEqualTo(FILES_TO_ADD.size());
}
 
Example #12
Source File: ZipInputStreamIT.java    From zip4j with Apache License 2.0 6 votes vote down vote up
private void extractZipFileWithInputStreams(File zipFile, char[] password, int bufferLength, AesVersion aesVersion)
    throws IOException {
  LocalFileHeader localFileHeader;
  int readLen;
  byte[] readBuffer = new byte[bufferLength];
  int numberOfEntriesExtracted = 0;

  try (FileInputStream fileInputStream = new FileInputStream(zipFile)) {
    try (ZipInputStream zipInputStream = new ZipInputStream(fileInputStream, password)) {
      while ((localFileHeader = zipInputStream.getNextEntry()) != null) {
        File extractedFile = temporaryFolder.newFile(localFileHeader.getFileName());
        try (OutputStream outputStream = new FileOutputStream(extractedFile)) {
          while ((readLen = zipInputStream.read(readBuffer)) != -1) {
            outputStream.write(readBuffer, 0, readLen);
          }
        }
        verifyLocalFileHeader(localFileHeader);
        verifyFileContent(getTestFileFromResources(localFileHeader.getFileName()), extractedFile);
        numberOfEntriesExtracted++;
      }
    }
  }

  assertThat(numberOfEntriesExtracted).isEqualTo(FILES_TO_ADD.size());
}
 
Example #13
Source File: HeaderReader.java    From AndroidZip with Apache License 2.0 6 votes vote down vote up
/**
 * Reads extra data record and saves it in the {@link LocalFileHeader} 
 * @param localFileHeader
 * @throws ZipException
 */
private void readAndSaveExtraDataRecord(LocalFileHeader localFileHeader) throws ZipException {
	
	if (zip4jRaf == null) {
		throw new ZipException("invalid file handler when trying to read extra data record");
	}
	
	if (localFileHeader == null) {
		throw new ZipException("file header is null");
	}
	
	int extraFieldLength = localFileHeader.getExtraFieldLength(); 
	if (extraFieldLength <= 0) {
		return;
	}
	
	localFileHeader.setExtraDataRecords(readExtraDataRecords(extraFieldLength));
	
}
 
Example #14
Source File: HeaderReader.java    From AndroidZip with Apache License 2.0 6 votes vote down vote up
/**
 * Reads Zip64 Extended Info and saves it in the {@link LocalFileHeader}
 * @param localFileHeader
 * @throws ZipException
 */
private void readAndSaveZip64ExtendedInfo(LocalFileHeader localFileHeader) throws ZipException {
	if (localFileHeader == null) {
		throw new ZipException("file header is null in reading Zip64 Extended Info");
	}
	
	if (localFileHeader.getExtraDataRecords() == null || localFileHeader.getExtraDataRecords().size() <= 0) {
		return;
	}
	
	Zip64ExtendedInfo zip64ExtendedInfo = readZip64ExtendedInfo(
			localFileHeader.getExtraDataRecords(), 
			localFileHeader.getUncompressedSize(), 
			localFileHeader.getCompressedSize(), 
			-1, -1);
	
	if (zip64ExtendedInfo != null) {
		localFileHeader.setZip64ExtendedInfo(zip64ExtendedInfo);
		
		if (zip64ExtendedInfo.getUnCompressedSize() != -1)
			localFileHeader.setUncompressedSize(zip64ExtendedInfo.getUnCompressedSize());
		
		if (zip64ExtendedInfo.getCompressedSize() != -1)
			localFileHeader.setCompressedSize(zip64ExtendedInfo.getCompressedSize());
	}
}
 
Example #15
Source File: HeaderReader.java    From AndroidZip with Apache License 2.0 6 votes vote down vote up
/**
 * Reads AES Extra Data Record and saves it in the {@link LocalFileHeader}
 * @param localFileHeader
 * @throws ZipException
 */
private void readAndSaveAESExtraDataRecord(LocalFileHeader localFileHeader) throws ZipException {
	if (localFileHeader == null) {
		throw new ZipException("file header is null in reading Zip64 Extended Info");
	}
	
	if (localFileHeader.getExtraDataRecords() == null || localFileHeader.getExtraDataRecords().size() <= 0) {
		return;
	}
	
	AESExtraDataRecord aesExtraDataRecord = readAESExtraDataRecord(localFileHeader.getExtraDataRecords());
	if (aesExtraDataRecord != null) {
		localFileHeader.setAesExtraDataRecord(aesExtraDataRecord);
		localFileHeader.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);
	}
}
 
Example #16
Source File: FileHeaderFactoryTest.java    From zip4j with Apache License 2.0 6 votes vote down vote up
private void verifyLocalFileHeader(LocalFileHeader localFileHeader, long lastModifiedFileTime) {
  assertThat(localFileHeader).isNotNull();
  assertThat(localFileHeader.getSignature()).isEqualTo(HeaderSignature.LOCAL_FILE_HEADER);
  assertThat(localFileHeader.getVersionNeededToExtract()).isEqualTo(20);
  assertThat(localFileHeader.getCompressionMethod()).isEqualTo(CompressionMethod.STORE);
  assertThat(localFileHeader.getLastModifiedTime()).isEqualTo(lastModifiedFileTime);
  assertThat(localFileHeader.getUncompressedSize()).isEqualTo(1000L);
  assertThat(localFileHeader.getFileName()).isEqualTo(FILE_NAME_IN_ZIP);
  assertThat(localFileHeader.getFileNameLength()).isEqualTo(FILE_NAME_IN_ZIP.length());
  assertThat(localFileHeader.isEncrypted()).isEqualTo(true);
  assertThat(localFileHeader.getEncryptionMethod()).isEqualTo(EncryptionMethod.AES);
  assertThat(localFileHeader.getCrc()).isEqualTo(1231231L);
  assertThat(localFileHeader.getCompressedSize()).isEqualTo(23523L);
  assertThat(localFileHeader.getGeneralPurposeFlag()).containsExactly(2, 28);
  assertThat(localFileHeader.isDataDescriptorExists()).isTrue();
  assertThat(localFileHeader.getExtraFieldLength()).isEqualTo(190);
}
 
Example #17
Source File: CipherOutputStream.java    From AndroidZip with Apache License 2.0 6 votes vote down vote up
private void createLocalFileHeader() throws ZipException {
	if (fileHeader == null) {
		throw new ZipException("file header is null, cannot create local file header");
	}
	this.localFileHeader = new LocalFileHeader();
	localFileHeader.setSignature((int)InternalZipConstants.LOCSIG);
	localFileHeader.setVersionNeededToExtract(fileHeader.getVersionNeededToExtract());
	localFileHeader.setCompressionMethod(fileHeader.getCompressionMethod());
	localFileHeader.setLastModFileTime(fileHeader.getLastModFileTime());
	localFileHeader.setUncompressedSize(fileHeader.getUncompressedSize());
	localFileHeader.setFileNameLength(fileHeader.getFileNameLength());
	localFileHeader.setFileName(fileHeader.getFileName());
	localFileHeader.setEncrypted(fileHeader.isEncrypted());
	localFileHeader.setEncryptionMethod(fileHeader.getEncryptionMethod());
	localFileHeader.setAesExtraDataRecord(fileHeader.getAesExtraDataRecord());
	localFileHeader.setCrc32(fileHeader.getCrc32());
	localFileHeader.setCompressedSize(fileHeader.getCompressedSize());
	localFileHeader.setGeneralPurposeFlag((byte[])fileHeader.getGeneralPurposeFlag().clone());
}
 
Example #18
Source File: FileHeaderFactory.java    From zip4j with Apache License 2.0 6 votes vote down vote up
public LocalFileHeader generateLocalFileHeader(FileHeader fileHeader) {
  LocalFileHeader localFileHeader = new LocalFileHeader();
  localFileHeader.setSignature(HeaderSignature.LOCAL_FILE_HEADER);
  localFileHeader.setVersionNeededToExtract(fileHeader.getVersionNeededToExtract());
  localFileHeader.setCompressionMethod(fileHeader.getCompressionMethod());
  localFileHeader.setLastModifiedTime(fileHeader.getLastModifiedTime());
  localFileHeader.setUncompressedSize(fileHeader.getUncompressedSize());
  localFileHeader.setFileNameLength(fileHeader.getFileNameLength());
  localFileHeader.setFileName(fileHeader.getFileName());
  localFileHeader.setEncrypted(fileHeader.isEncrypted());
  localFileHeader.setEncryptionMethod(fileHeader.getEncryptionMethod());
  localFileHeader.setAesExtraDataRecord(fileHeader.getAesExtraDataRecord());
  localFileHeader.setCrc(fileHeader.getCrc());
  localFileHeader.setCompressedSize(fileHeader.getCompressedSize());
  localFileHeader.setGeneralPurposeFlag(fileHeader.getGeneralPurposeFlag().clone());
  localFileHeader.setDataDescriptorExists(fileHeader.isDataDescriptorExists());
  localFileHeader.setExtraFieldLength(fileHeader.getExtraFieldLength());
  return localFileHeader;
}
 
Example #19
Source File: HeaderWriter.java    From AndroidZip with Apache License 2.0 5 votes vote down vote up
public int writeExtendedLocalHeader(LocalFileHeader localFileHeader, 
		OutputStream outputStream) throws ZipException, IOException {
	if (localFileHeader == null || outputStream == null) {
		throw new ZipException("input parameters is null, cannot write extended local header");
	}
	
	ArrayList byteArrayList = new ArrayList();
	byte[] intByte = new byte[4];
	
	//Extended local file header signature
	Raw.writeIntLittleEndian(intByte, 0, (int)InternalZipConstants.EXTSIG);
	copyByteArrayToArrayList(intByte, byteArrayList);
	
	//CRC
	Raw.writeIntLittleEndian(intByte, 0, (int)localFileHeader.getCrc32());
	copyByteArrayToArrayList(intByte, byteArrayList);
	
	//compressed size
	long compressedSize = localFileHeader.getCompressedSize();
	if (compressedSize >= Integer.MAX_VALUE) {
		compressedSize = Integer.MAX_VALUE;
	}
	Raw.writeIntLittleEndian(intByte, 0, (int)compressedSize);
	copyByteArrayToArrayList(intByte, byteArrayList);
	
	//uncompressed size 
	long uncompressedSize = localFileHeader.getUncompressedSize();
	if (uncompressedSize >= Integer.MAX_VALUE) {
		uncompressedSize = Integer.MAX_VALUE;
	}
	Raw.writeIntLittleEndian(intByte, 0, (int)uncompressedSize);
	copyByteArrayToArrayList(intByte, byteArrayList);
	
	byte[] extLocHdrBytes = byteArrayListToByteArray(byteArrayList);
	outputStream.write(extLocHdrBytes);
	return extLocHdrBytes.length;
}
 
Example #20
Source File: HeaderReader.java    From zip4j with Apache License 2.0 5 votes vote down vote up
private void readExtraDataRecords(InputStream inputStream, LocalFileHeader localFileHeader)
    throws IOException {
  int extraFieldLength = localFileHeader.getExtraFieldLength();
  if (extraFieldLength <= 0) {
    return;
  }

  localFileHeader.setExtraDataRecords(readExtraDataRecords(inputStream, extraFieldLength));

}
 
Example #21
Source File: Zip4jUtilTest.java    From zip4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetCompressionMethodForAesWhenAesExtraDataMissingThrowsException() {
  expectedException.expectMessage("AesExtraDataRecord not present in local header for aes encrypted data");
  expectedException.expect(RuntimeException.class);

  LocalFileHeader localFileHeader = new LocalFileHeader();
  localFileHeader.setCompressionMethod(CompressionMethod.AES_INTERNAL_ONLY);

  Zip4jUtil.getCompressionMethod(localFileHeader);
}
 
Example #22
Source File: Zip4jUtilTest.java    From zip4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetCompressionMethodForNonAesReturnsAsIs() {
  LocalFileHeader localFileHeader = new LocalFileHeader();
  localFileHeader.setCompressionMethod(CompressionMethod.DEFLATE);

  assertThat(Zip4jUtil.getCompressionMethod(localFileHeader)).isEqualTo(CompressionMethod.DEFLATE);
}
 
Example #23
Source File: Zip4jUtil.java    From zip4j with Apache License 2.0 5 votes vote down vote up
public static CompressionMethod getCompressionMethod(LocalFileHeader localFileHeader) {
  if (localFileHeader.getCompressionMethod() != CompressionMethod.AES_INTERNAL_ONLY) {
    return localFileHeader.getCompressionMethod();
  }

  if (localFileHeader.getAesExtraDataRecord() == null) {
    throw new RuntimeException("AesExtraDataRecord not present in local header for aes encrypted data");
  }

  return localFileHeader.getAesExtraDataRecord().getCompressionMethod();
}
 
Example #24
Source File: HeaderReaderIT.java    From zip4j with Apache License 2.0 5 votes vote down vote up
private LocalFileHeader generateLocalFileHeader(long entrySize, EncryptionMethod encryptionMethod)
    throws ZipException {
  List<FileHeader> fileHeaders = generateFileHeaders(generateZipParameters(encryptionMethod), 1);
  LocalFileHeader localFileHeader = fileHeaderFactory.generateLocalFileHeader(fileHeaders.get(0));
  localFileHeader.setCompressedSize(entrySize);
  localFileHeader.setUncompressedSize(entrySize);
  return localFileHeader;
}
 
Example #25
Source File: HeaderReaderIT.java    From zip4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testReadLocalFileHeaderWithAesEncryption() throws IOException {
  long entrySize = InternalZipConstants.ZIP_64_SIZE_LIMIT - 1001 ;
  File headerFile = generateAndWriteLocalFileHeader(entrySize, EncryptionMethod.AES);

  try(InputStream inputStream = new FileInputStream(headerFile)) {
    LocalFileHeader readLocalFileHeader = headerReader.readLocalFileHeader(inputStream, InternalZipConstants.CHARSET_UTF_8);
    assertThat(readLocalFileHeader).isNotNull();
    assertThat(readLocalFileHeader.getCompressedSize()).isEqualTo(entrySize);
    assertThat(readLocalFileHeader.getUncompressedSize()).isEqualTo(entrySize);
    assertThat(readLocalFileHeader.getAesExtraDataRecord()).isNotNull();
    assertThat(readLocalFileHeader.getAesExtraDataRecord().getAesKeyStrength())
        .isEqualTo(AesKeyStrength.KEY_STRENGTH_256);
  }
}
 
Example #26
Source File: HeaderReaderIT.java    From zip4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testReadLocalFileHeader() throws IOException {
  long entrySize = InternalZipConstants.ZIP_64_SIZE_LIMIT + 1;
  File headerFile = generateAndWriteLocalFileHeader(entrySize, EncryptionMethod.NONE);

  try(InputStream inputStream = new FileInputStream(headerFile)) {
    LocalFileHeader readLocalFileHeader = headerReader.readLocalFileHeader(inputStream, InternalZipConstants.CHARSET_UTF_8);
    assertThat(readLocalFileHeader).isNotNull();
    assertThat(readLocalFileHeader.getCompressedSize()).isEqualTo(entrySize);
    assertThat(readLocalFileHeader.getUncompressedSize()).isEqualTo(entrySize);
  }
}
 
Example #27
Source File: ZipInputStream.java    From zip4j with Apache License 2.0 5 votes vote down vote up
private DecompressedInputStream initializeDecompressorForThisEntry(CipherInputStream cipherInputStream, LocalFileHeader localFileHeader) {
  CompressionMethod compressionMethod = getCompressionMethod(localFileHeader);

  if (compressionMethod == CompressionMethod.DEFLATE) {
    return new InflaterInputStream(cipherInputStream);
  }

  return new StoreInputStream(cipherInputStream);
}
 
Example #28
Source File: HeaderWriterIT.java    From zip4j with Apache License 2.0 5 votes vote down vote up
private LocalFileHeader createLocalFileHeader(String fileNameSuffix, long compressedSize, long uncompressedSize,
                                              boolean useUtf8) {
  LocalFileHeader localFileHeader = new LocalFileHeader();
  localFileHeader.setVersionNeededToExtract(VERSION_NEEDED_TO_EXTRACT);
  localFileHeader.setFileName(FILE_NAME_PREFIX + fileNameSuffix);
  localFileHeader.setCompressedSize(compressedSize);
  localFileHeader.setUncompressedSize(uncompressedSize);
  localFileHeader.setGeneralPurposeFlag(generateGeneralPurposeBytes(useUtf8));
  localFileHeader.setCompressionMethod(CompressionMethod.DEFLATE);
  localFileHeader.setLastModifiedTime(LAST_MODIFIED_FILE_TIME);
  return localFileHeader;
}
 
Example #29
Source File: HeaderWriterIT.java    From zip4j with Apache License 2.0 5 votes vote down vote up
private void verifyLocalFileHeader(LocalFileHeader localFileHeader, String expectedFileName, long compressedSize,
                                   long uncompressedSize) {
  assertThat(localFileHeader).isNotNull();
  assertThat(localFileHeader.getVersionNeededToExtract()).isEqualTo(VERSION_NEEDED_TO_EXTRACT);
  assertThat(localFileHeader.getFileName()).isEqualTo(expectedFileName);
  assertThat(localFileHeader.getCompressedSize()).isEqualTo(compressedSize);
  assertThat(localFileHeader.getUncompressedSize()).isEqualTo(uncompressedSize);
  assertThat(localFileHeader.getCompressionMethod()).isEqualTo(CompressionMethod.DEFLATE);
  assertThat(localFileHeader.getLastModifiedTime()).isEqualTo(LAST_MODIFIED_FILE_TIME);
}
 
Example #30
Source File: HeaderVerifier.java    From zip4j with Apache License 2.0 5 votes vote down vote up
private static LocalFileHeader getLocalFileHeaderForEntry(File generatedZipFile, String fileNameInZipToVerify)
    throws IOException {

  InputStream inputStream = positionRandomAccessFileToLocalFileHeaderStart(generatedZipFile,
      fileNameInZipToVerify);
  return headerReader.readLocalFileHeader(inputStream, InternalZipConstants.CHARSET_UTF_8);
}