Java Code Examples for net.lingala.zip4j.model.FileHeader#setFileName()
The following examples show how to use
net.lingala.zip4j.model.FileHeader#setFileName() .
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: HeaderWriterIT.java From zip4j with Apache License 2.0 | 6 votes |
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 2
Source File: HeaderWriterIT.java From zip4j with Apache License 2.0 | 6 votes |
private List<FileHeader> createFileHeaders(int numberOfEntriesInCentralDirectory, long compressedSize, long uncompressedSize) { List<FileHeader> fileHeaders = new ArrayList<>(); for (int i = 0; i < numberOfEntriesInCentralDirectory; i++) { FileHeader fileHeader = new FileHeader(); fileHeader.setVersionMadeBy(VERSION_MADE_BY); fileHeader.setVersionNeededToExtract(VERSION_NEEDED_TO_EXTRACT); fileHeader.setFileName(FILE_NAME_PREFIX + i); fileHeader.setGeneralPurposeFlag(generateGeneralPurposeBytes(true)); fileHeader.setCompressedSize(compressedSize); fileHeader.setUncompressedSize(uncompressedSize); fileHeader.setCompressionMethod(CompressionMethod.DEFLATE); fileHeader.setExternalFileAttributes(EXTERNAL_FILE_ATTRIBUTES); fileHeader.setFileComment(FILE_COMMENT_PREFIX + i); fileHeaders.add(fileHeader); } return fileHeaders; }
Example 3
Source File: FileHeaderFactoryTest.java From zip4j with Apache License 2.0 | 6 votes |
private FileHeader generateFileHeader(long lastModifiedFileTime) { FileHeader fileHeader = new FileHeader(); fileHeader.setVersionNeededToExtract(20); fileHeader.setCompressionMethod(CompressionMethod.STORE); fileHeader.setLastModifiedTime(lastModifiedFileTime); fileHeader.setUncompressedSize(1000L); fileHeader.setFileName(FILE_NAME_IN_ZIP); fileHeader.setFileNameLength(FILE_NAME_IN_ZIP.getBytes(InternalZipConstants.CHARSET_UTF_8).length); fileHeader.setEncrypted(true); fileHeader.setEncryptionMethod(EncryptionMethod.AES); fileHeader.setCrc(1231231L); fileHeader.setCompressedSize(23523L); fileHeader.setGeneralPurposeFlag(new byte[] {2, 28}); fileHeader.setDataDescriptorExists(true); fileHeader.setExtraFieldLength(190); return fileHeader; }
Example 4
Source File: HeaderUtilTest.java From zip4j with Apache License 2.0 | 6 votes |
@Test public void testGetIndexOfFileHeaderGetsIndexSuccessfully() throws ZipException { String fileNamePrefix = "FILE_NAME_"; int numberOfEntriesToAdd = 10; List<FileHeader> fileHeadersInZipModel = generateFileHeaderWithFileNamesWithEmptyAndNullFileNames(fileNamePrefix, numberOfEntriesToAdd); ZipModel zipModel = new ZipModel(); zipModel.getCentralDirectory().setFileHeaders(fileHeadersInZipModel); FileHeader fileHeaderToFind = new FileHeader(); for (int i = 0; i < numberOfEntriesToAdd; i++) { fileHeaderToFind.setFileName(fileNamePrefix + i); assertThat(HeaderUtil.getIndexOfFileHeader(zipModel, fileHeaderToFind)).isEqualTo(i); } fileHeaderToFind.setFileName(fileNamePrefix + numberOfEntriesToAdd); assertThat(HeaderUtil.getIndexOfFileHeader(zipModel, fileHeaderToFind)).isEqualTo(-1); }
Example 5
Source File: RenameFilesTask.java From zip4j with Apache License 2.0 | 5 votes |
private void updateHeadersInZipModel(FileHeader fileHeader, String newFileName, byte[] newFileNameBytes, int headersOffset) throws ZipException { FileHeader fileHeaderToBeChanged = HeaderUtil.getFileHeader(zipModel, fileHeader.getFileName()); if (fileHeaderToBeChanged == null) { // If this is the case, then the file name in the header that was passed to this method was already changed. // In theory, should never be here. throw new ZipException("could not find any header with name: " + fileHeader.getFileName()); } fileHeaderToBeChanged.setFileName(newFileName); fileHeaderToBeChanged.setFileNameLength(newFileNameBytes.length); updateOffsetsForAllSubsequentFileHeaders(zipModel, fileHeaderToBeChanged, headersOffset); zipModel.getEndOfCentralDirectoryRecord().setOffsetOfStartOfCentralDirectory( zipModel.getEndOfCentralDirectoryRecord().getOffsetOfStartOfCentralDirectory() + headersOffset); if (zipModel.isZip64Format()) { zipModel.getZip64EndOfCentralDirectoryRecord().setOffsetStartCentralDirectoryWRTStartDiskNumber( zipModel.getZip64EndOfCentralDirectoryRecord().getOffsetStartCentralDirectoryWRTStartDiskNumber() + headersOffset ); zipModel.getZip64EndOfCentralDirectoryLocator().setOffsetZip64EndOfCentralDirectoryRecord( zipModel.getZip64EndOfCentralDirectoryLocator().getOffsetZip64EndOfCentralDirectoryRecord() + headersOffset ); } }
Example 6
Source File: HeaderUtilTest.java From zip4j with Apache License 2.0 | 5 votes |
@Test public void testGetIndexOfFileHeaderWithEmptyFileNameInFileHeaderThrowsException() throws ZipException { expectZipException("file name in file header is empty or null, cannot determine index of file header"); ZipModel zipModel = new ZipModel(); zipModel.getCentralDirectory().setFileHeaders(Collections.singletonList(new FileHeader())); FileHeader fileHeader = new FileHeader(); fileHeader.setFileName(""); HeaderUtil.getIndexOfFileHeader(zipModel, new FileHeader()); }
Example 7
Source File: HeaderReaderIT.java From zip4j with Apache License 2.0 | 5 votes |
private void testWithoutUtf8FileName(String fileName, String entryComment, boolean shouldFileNamesMatch, boolean unsetUtf8Flag, Charset charsetToUseForReading) throws IOException { ZipModel actualZipModel = generateZipModel(3); FileHeader secondFileHeader = actualZipModel.getCentralDirectory().getFileHeaders().get(1); if (unsetUtf8Flag) { // Unset utf8 flag byte[] generalPurposeBytes = secondFileHeader.getGeneralPurposeFlag(); generalPurposeBytes[1] = BitUtils.unsetBit(generalPurposeBytes[1], 3); secondFileHeader.setGeneralPurposeFlag(generalPurposeBytes); } secondFileHeader.setFileName(fileName); secondFileHeader.setFileComment(entryComment); File headersFile = writeZipHeaders(actualZipModel); actualZipModel.setZipFile(headersFile); try(RandomAccessFile randomAccessFile = new RandomAccessFile(actualZipModel.getZipFile(), RandomAccessFileMode.READ.getValue())) { ZipModel readZipModel = headerReader.readAllHeaders(randomAccessFile, charsetToUseForReading); FileHeader fileHeader = readZipModel.getCentralDirectory().getFileHeaders().get(1); if (shouldFileNamesMatch) { assertThat(fileHeader.getFileName()).isEqualTo(fileName); assertThat(fileHeader.getFileComment()).isEqualTo(entryComment); } else { assertThat(fileHeader.getFileName()).isNotEqualTo(fileName); assertThat(fileHeader.getFileComment()).isNotEqualTo(entryComment); } assertThat(readZipModel.getCentralDirectory().getFileHeaders().get(0).getFileCommentLength()).isEqualTo(0); assertThat(readZipModel.getCentralDirectory().getFileHeaders().get(0).getFileComment()).isNull(); assertThat(readZipModel.getCentralDirectory().getFileHeaders().get(2).getFileCommentLength()).isEqualTo(0); assertThat(readZipModel.getCentralDirectory().getFileHeaders().get(2).getFileComment()).isNull(); } }
Example 8
Source File: ZipFileTest.java From zip4j with Apache License 2.0 | 5 votes |
@Test public void testRenameFileWithFileHeaderThrowsExceptionWhenNewFileNameIsNull() throws ZipException { expectedException.expect(ZipException.class); expectedException.expectMessage("newFileName is null or empty"); FileHeader fileHeader = new FileHeader(); fileHeader.setFileName("somename"); zipFile.renameFile(fileHeader, null); }
Example 9
Source File: ZipFileTest.java From zip4j with Apache License 2.0 | 5 votes |
@Test public void testRenameFileWithFileHeaderThrowsExceptionWhenNewFileNameIsEmpty() throws ZipException { expectedException.expect(ZipException.class); expectedException.expectMessage("newFileName is null or empty"); FileHeader fileHeader = new FileHeader(); fileHeader.setFileName("somename"); zipFile.renameFile(fileHeader, ""); }
Example 10
Source File: FileHeaderFactory.java From zip4j with Apache License 2.0 | 4 votes |
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; }
Example 11
Source File: HeaderUtilTest.java From zip4j with Apache License 2.0 | 4 votes |
private FileHeader generateFileHeader(String fileName) { FileHeader fileHeader = new FileHeader(); fileHeader.setFileName(fileName); return fileHeader; }