Java Code Examples for net.lingala.zip4j.model.LocalFileHeader

The following examples show how to use net.lingala.zip4j.model.LocalFileHeader. These examples are extracted from open source projects. 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 Project: zip4j   Source File: FileHeaderFactory.java    License: 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 2
Source Project: zip4j   Source File: HeaderReader.java    License: 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 3
Source Project: zip4j   Source File: ZipInputStream.java    License: 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 4
Source Project: zip4j   Source File: HeaderWriterIT.java    License: 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 Project: zip4j   Source File: HeaderWriterIT.java    License: 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 Project: zip4j   Source File: HeaderWriterIT.java    License: 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 Project: zip4j   Source File: HeaderWriterIT.java    License: 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 Project: zip4j   Source File: HeaderWriterIT.java    License: 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 Project: zip4j   Source File: HeaderWriterIT.java    License: 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 Project: zip4j   Source File: FileHeaderFactoryTest.java    License: 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 11
Source Project: zip4j   Source File: HeaderReaderIT.java    License: 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 12
Source Project: zip4j   Source File: ZipInputStreamIT.java    License: 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 13
Source Project: zip4j   Source File: ZipInputStreamIT.java    License: 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 14
Source Project: AndroidZip   Source File: HeaderReader.java    License: 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 15
Source Project: AndroidZip   Source File: HeaderReader.java    License: 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 16
Source Project: AndroidZip   Source File: HeaderReader.java    License: 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 17
Source Project: AndroidZip   Source File: CipherOutputStream.java    License: 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
@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 19
Source Project: zip4j   Source File: AbstractExtractFileTask.java    License: Apache License 2.0 5 votes vote down vote up
private void verifyNextEntry(ZipInputStream zipInputStream, FileHeader fileHeader) throws IOException {
  LocalFileHeader localFileHeader = zipInputStream.getNextEntry(fileHeader);

  if (localFileHeader == null) {
    throw new ZipException("Could not read corresponding local file header for file header: "
        + fileHeader.getFileName());
  }

  if (!fileHeader.getFileName().equals(localFileHeader.getFileName())) {
    throw new ZipException("File header and local file header mismatch");
  }
}
 
Example 20
Source Project: zip4j   Source File: HeaderWriter.java    License: Apache License 2.0 5 votes vote down vote up
public void writeExtendedLocalHeader(LocalFileHeader localFileHeader, OutputStream outputStream)
    throws IOException {

  if (localFileHeader == null || outputStream == null) {
    throw new ZipException("input parameters is null, cannot write extended local header");
  }

  try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
    rawIO.writeIntLittleEndian(byteArrayOutputStream, (int) HeaderSignature.EXTRA_DATA_RECORD.getValue());

    rawIO.writeLongLittleEndian(longBuff, 0, localFileHeader.getCrc());
    byteArrayOutputStream.write(longBuff, 0, 4);

    if (localFileHeader.isWriteCompressedSizeInZip64ExtraRecord()) {
      rawIO.writeLongLittleEndian(byteArrayOutputStream, localFileHeader.getCompressedSize());
      rawIO.writeLongLittleEndian(byteArrayOutputStream, localFileHeader.getUncompressedSize());
    } else {
      rawIO.writeLongLittleEndian(longBuff, 0, localFileHeader.getCompressedSize());
      byteArrayOutputStream.write(longBuff, 0, 4);

      rawIO.writeLongLittleEndian(longBuff, 0, localFileHeader.getUncompressedSize());
      byteArrayOutputStream.write(longBuff, 0, 4);
    }

    outputStream.write(byteArrayOutputStream.toByteArray());
  }
}
 
Example 21
Source Project: zip4j   Source File: HeaderReader.java    License: 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 22
Source Project: zip4j   Source File: HeaderReader.java    License: Apache License 2.0 5 votes vote down vote up
private void readAesExtraDataRecord(LocalFileHeader localFileHeader, RawIO rawIO) throws ZipException {
  if (localFileHeader.getExtraDataRecords() == null || localFileHeader.getExtraDataRecords().size() <= 0) {
    return;
  }

  AESExtraDataRecord aesExtraDataRecord = readAesExtraDataRecord(localFileHeader.getExtraDataRecords(), rawIO);
  if (aesExtraDataRecord != null) {
    localFileHeader.setAesExtraDataRecord(aesExtraDataRecord);
    localFileHeader.setEncryptionMethod(EncryptionMethod.AES);
  }
}
 
Example 23
Source Project: zip4j   Source File: Zip4jUtil.java    License: 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 Project: zip4j   Source File: AesCipherInputStream.java    License: Apache License 2.0 5 votes vote down vote up
private byte[] getSalt(LocalFileHeader localFileHeader) throws IOException {
  if (localFileHeader.getAesExtraDataRecord() == null) {
    throw new IOException("invalid aes extra data record");
  }

  AESExtraDataRecord aesExtraDataRecord = localFileHeader.getAesExtraDataRecord();
  byte[] saltBytes = new byte[aesExtraDataRecord.getAesKeyStrength().getSaltLength()];
  readRaw(saltBytes);
  return saltBytes;
}
 
Example 25
Source Project: zip4j   Source File: ZipInputStream.java    License: Apache License 2.0 5 votes vote down vote up
private CipherInputStream initializeCipherInputStream(ZipEntryInputStream zipEntryInputStream, LocalFileHeader localFileHeader) throws IOException {
  if (!localFileHeader.isEncrypted()) {
    return new NoCipherInputStream(zipEntryInputStream, localFileHeader, password);
  }

  if (localFileHeader.getEncryptionMethod() == EncryptionMethod.AES) {
    return new AesCipherInputStream(zipEntryInputStream, localFileHeader, password);
  } else {
    return new ZipStandardCipherInputStream(zipEntryInputStream, localFileHeader, password);
  }
}
 
Example 26
Source Project: zip4j   Source File: ZipInputStream.java    License: 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 27
Source Project: zip4j   Source File: ZipInputStream.java    License: Apache License 2.0 5 votes vote down vote up
private void verifyLocalFileHeader(LocalFileHeader localFileHeader) throws IOException {
  if (!isEntryDirectory(localFileHeader.getFileName())
      && localFileHeader.getCompressionMethod() == CompressionMethod.STORE
      && localFileHeader.getUncompressedSize() < 0) {
    throw new IOException("Invalid local file header for: " + localFileHeader.getFileName()
        + ". Uncompressed size has to be set for entry of compression type store which is not a directory");
  }
}
 
Example 28
Source Project: zip4j   Source File: ZipInputStream.java    License: Apache License 2.0 5 votes vote down vote up
private long getCompressedSize(LocalFileHeader localFileHeader) {
  if (getCompressionMethod(localFileHeader).equals(CompressionMethod.STORE)) {
    return localFileHeader.getUncompressedSize();
  }

  if (localFileHeader.isDataDescriptorExists() && !canSkipExtendedLocalFileHeader) {
    return -1;
  }

  return localFileHeader.getCompressedSize() - getEncryptionHeaderSize(localFileHeader);
}
 
Example 29
Source Project: zip4j   Source File: ZipInputStream.java    License: Apache License 2.0 5 votes vote down vote up
private int getEncryptionHeaderSize(LocalFileHeader localFileHeader) {
  if (!localFileHeader.isEncrypted()) {
    return 0;
  }

  if (localFileHeader.getEncryptionMethod().equals(EncryptionMethod.AES)) {
    return InternalZipConstants.AES_AUTH_LENGTH + AESDecrypter.PASSWORD_VERIFIER_LENGTH
        + localFileHeader.getAesExtraDataRecord().getAesKeyStrength().getSaltLength();
  } else if (localFileHeader.getEncryptionMethod().equals(EncryptionMethod.ZIP_STANDARD)) {
    return InternalZipConstants.STD_DEC_HDR_SIZE;
  } else {
    return 0;
  }
}
 
Example 30
Source Project: zip4j   Source File: CipherInputStream.java    License: Apache License 2.0 5 votes vote down vote up
public CipherInputStream(ZipEntryInputStream zipEntryInputStream, LocalFileHeader localFileHeader, char[] password) throws IOException, ZipException {
  this.zipEntryInputStream = zipEntryInputStream;
  this.decrypter = initializeDecrypter(localFileHeader, password);
  this.localFileHeader = localFileHeader;

  if (getCompressionMethod(localFileHeader) == CompressionMethod.DEFLATE) {
    lastReadRawDataCache = new byte[InternalZipConstants.BUFF_SIZE];
  }
}