Java Code Examples for net.lingala.zip4j.model.LocalFileHeader#setEncryptionMethod()
The following examples show how to use
net.lingala.zip4j.model.LocalFileHeader#setEncryptionMethod() .
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: FileHeaderFactory.java From zip4j with Apache License 2.0 | 6 votes |
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 File: HeaderWriterIT.java From zip4j with Apache License 2.0 | 6 votes |
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 3
Source File: HeaderReaderIT.java From zip4j with Apache License 2.0 | 6 votes |
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 4
Source File: HeaderReader.java From AndroidZip with Apache License 2.0 | 6 votes |
/** * 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 5
Source File: HeaderReader.java From zip4j with Apache License 2.0 | 5 votes |
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 6
Source File: HeaderReader.java From zip4j with Apache License 2.0 | 4 votes |
public LocalFileHeader readLocalFileHeader(InputStream inputStream, Charset charset) throws IOException { LocalFileHeader localFileHeader = new LocalFileHeader(); byte[] intBuff = new byte[4]; //signature int sig = rawIO.readIntLittleEndian(inputStream); if (sig != HeaderSignature.LOCAL_FILE_HEADER.getValue()) { return null; } localFileHeader.setSignature(HeaderSignature.LOCAL_FILE_HEADER); localFileHeader.setVersionNeededToExtract(rawIO.readShortLittleEndian(inputStream)); byte[] generalPurposeFlags = new byte[2]; if (readFully(inputStream, generalPurposeFlags) != 2) { throw new ZipException("Could not read enough bytes for generalPurposeFlags"); } localFileHeader.setEncrypted(isBitSet(generalPurposeFlags[0], 0)); localFileHeader.setDataDescriptorExists(isBitSet(generalPurposeFlags[0], 3)); localFileHeader.setFileNameUTF8Encoded(isBitSet(generalPurposeFlags[1], 3)); localFileHeader.setGeneralPurposeFlag(generalPurposeFlags.clone()); localFileHeader.setCompressionMethod(CompressionMethod.getCompressionMethodFromCode( rawIO.readShortLittleEndian(inputStream))); localFileHeader.setLastModifiedTime(rawIO.readIntLittleEndian(inputStream)); readFully(inputStream, intBuff); localFileHeader.setCrc(rawIO.readLongLittleEndian(intBuff, 0)); localFileHeader.setCrcRawData(intBuff.clone()); localFileHeader.setCompressedSize(rawIO.readLongLittleEndian(inputStream, 4)); localFileHeader.setUncompressedSize(rawIO.readLongLittleEndian(inputStream, 4)); int fileNameLength = rawIO.readShortLittleEndian(inputStream); localFileHeader.setFileNameLength(fileNameLength); localFileHeader.setExtraFieldLength(rawIO.readShortLittleEndian(inputStream)); if (fileNameLength > 0) { byte[] fileNameBuf = new byte[fileNameLength]; readFully(inputStream, fileNameBuf); // Modified after user reported an issue http://www.lingala.net/zip4j/forum/index.php?topic=2.0 // String fileName = new String(fileNameBuf, "Cp850"); // String fileName = Zip4jUtil.getCp850EncodedString(fileNameBuf); String fileName = decodeStringWithCharset(fileNameBuf, localFileHeader.isFileNameUTF8Encoded(), charset); if (fileName == null) { throw new ZipException("file name is null, cannot assign file name to local file header"); } if (fileName.contains(":" + System.getProperty("file.separator"))) { fileName = fileName.substring(fileName.indexOf(":" + System.getProperty("file.separator")) + 2); } localFileHeader.setFileName(fileName); localFileHeader.setDirectory(fileName.endsWith("/") || fileName.endsWith("\\")); } else { localFileHeader.setFileName(null); } readExtraDataRecords(inputStream, localFileHeader); readZip64ExtendedInfo(localFileHeader, rawIO); readAesExtraDataRecord(localFileHeader, rawIO); if (localFileHeader.isEncrypted()) { if (localFileHeader.getEncryptionMethod() == EncryptionMethod.AES) { //Do nothing } else { if (BigInteger.valueOf(localFileHeader.getGeneralPurposeFlag()[0]).testBit(6)) { localFileHeader.setEncryptionMethod(EncryptionMethod.ZIP_STANDARD_VARIANT_STRONG); } else { localFileHeader.setEncryptionMethod(EncryptionMethod.ZIP_STANDARD); } } } return localFileHeader; }