Java Code Examples for net.lingala.zip4j.model.ZipParameters#setFileNameInZip()
The following examples show how to use
net.lingala.zip4j.model.ZipParameters#setFileNameInZip() .
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: ZipOutputStreamIT.java From zip4j with Apache License 2.0 | 6 votes |
private void testAddCommentToZipOutputStreamWithCharset(Charset charset, String comment) throws IOException { ZipParameters zipParameters = new ZipParameters(); byte[] buff = new byte[4096]; int readLen; List<File> filesToAdd = FILES_TO_ADD; try(ZipOutputStream zos = initializeZipOutputStream(false, charset)) { for (int i = 0; i < filesToAdd.size(); i++) { File fileToAdd = filesToAdd.get(i); zipParameters.setFileNameInZip(fileToAdd.getName()); zos.putNextEntry(zipParameters); try(InputStream inputStream = new FileInputStream(fileToAdd)) { while ((readLen = inputStream.read(buff)) != -1) { zos.write(buff, 0, readLen); } } zos.closeEntry(); } zos.setComment(comment); } verifyZipFileByExtractingAllFiles(generatedZipFile, PASSWORD, outputFolder, filesToAdd.size(), true, charset); verifyEntries(); verifyZipComment(comment, charset); }
Example 2
Source File: AddFilesToZipIT.java From zip4j with Apache License 2.0 | 6 votes |
@Test public void testAddStreamToZipWithAesEncryptionV1ForExistingZipAddsSuccessfully() throws IOException { File fileToAdd = TestUtils.getTestFileFromResources("가나다.abc"); ZipParameters zipParameters = createZipParameters(EncryptionMethod.AES, AesKeyStrength.KEY_STRENGTH_256); zipParameters.setAesVersion(AesVersion.ONE); zipParameters.setFileNameInZip(fileToAdd.getName()); ZipFile zipFile = new ZipFile(generatedZipFile, PASSWORD); zipFile.addFiles(FILES_TO_ADD); InputStream inputStream = new FileInputStream(fileToAdd); zipFile.addStream(inputStream, zipParameters); ZipFileVerifier.verifyZipFileByExtractingAllFiles(generatedZipFile, PASSWORD, outputFolder, 4); verifyFileIsOf(generatedZipFile, "가나다.abc", CompressionMethod.DEFLATE, EncryptionMethod.AES, AesKeyStrength.KEY_STRENGTH_256, AesVersion.ONE); }
Example 3
Source File: AddFilesToZipIT.java From zip4j with Apache License 2.0 | 6 votes |
@Test public void testAddStreamWithStoreCompressionAndDefaultCharset() throws IOException { String koreanFileName = "가나다.abc"; File fileToAdd = TestUtils.getTestFileFromResources(koreanFileName); ZipParameters zipParameters = new ZipParameters(); zipParameters.setCompressionMethod(CompressionMethod.STORE); zipParameters.setFileNameInZip(fileToAdd.getName()); ZipFile zipFile = new ZipFile(generatedZipFile); InputStream inputStream = new FileInputStream(fileToAdd); zipFile.addStream(inputStream, zipParameters); byte[] generalPurposeBytes = zipFile.getFileHeaders().get(0).getGeneralPurposeFlag(); // assert that extra data record is not present assertThat(BitUtils.isBitSet(generalPurposeBytes[1], 3)).isTrue(); assertThat(zipFile.getFileHeaders().get(0).getFileName()).isEqualTo(koreanFileName); }
Example 4
Source File: ZipUtil.java From document-management-software with GNU Lesser General Public License v3.0 | 6 votes |
public static void addEntry(File zip, String entry, InputStream content) { try { // read war.zip and write to ZipFile zipFile = new ZipFile(zip); ZipParameters parameters = new ZipParameters(); parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); // this would be the name of the file for this entry in the zip file parameters.setFileNameInZip(entry); // we set this flag to true. If this flag is true, Zip4j identifies // that // the data will not be from a file but directly from a stream parameters.setSourceExternalStream(true); // Creates a new entry in the zip file and adds the content to the // zip // file zipFile.addStream(content, parameters); } catch (Exception e) { logError(e.getMessage()); } }
Example 5
Source File: AddFilesToZipIT.java From zip4j with Apache License 2.0 | 6 votes |
@Test public void testAddStreamToWithStoreCompressionAndZipStandardEncryption() throws IOException { File fileToAdd = TestUtils.getTestFileFromResources("sample_text_large.txt"); ZipParameters zipParameters = createZipParameters(EncryptionMethod.ZIP_STANDARD, null); zipParameters.setCompressionMethod(CompressionMethod.STORE); zipParameters.setFileNameInZip(fileToAdd.getName()); ZipFile zipFile = new ZipFile(generatedZipFile, PASSWORD); InputStream inputStream = new FileInputStream(fileToAdd); zipFile.addStream(inputStream, zipParameters); ZipFileVerifier.verifyZipFileByExtractingAllFiles(generatedZipFile, PASSWORD, outputFolder, 1); verifyZipFileContainsFiles(generatedZipFile, singletonList("sample_text_large.txt"), CompressionMethod.STORE, EncryptionMethod.ZIP_STANDARD, null); zipFile = new ZipFile(generatedZipFile, PASSWORD); byte[] generalPurposeBytes = zipFile.getFileHeaders().get(0).getGeneralPurposeFlag(); // assert that extra data record is not present assertThat(BitUtils.isBitSet(generalPurposeBytes[0], 3)).isTrue(); }
Example 6
Source File: AddFilesToZipIT.java From zip4j with Apache License 2.0 | 6 votes |
@Test public void testAddStreamToWithStoreCompressionAndWithoutEncryption() throws IOException { File fileToAdd = TestUtils.getTestFileFromResources("бореиская.txt"); ZipParameters zipParameters = new ZipParameters(); zipParameters.setCompressionMethod(CompressionMethod.STORE); zipParameters.setFileNameInZip(fileToAdd.getName()); ZipFile zipFile = new ZipFile(generatedZipFile); InputStream inputStream = new FileInputStream(fileToAdd); zipFile.addStream(inputStream, zipParameters); ZipFileVerifier.verifyZipFileByExtractingAllFiles(generatedZipFile, outputFolder, 1); verifyZipFileContainsFiles(generatedZipFile, singletonList("бореиская.txt"), CompressionMethod.STORE, null, null); zipFile = new ZipFile(generatedZipFile); byte[] generalPurposeBytes = zipFile.getFileHeaders().get(0).getGeneralPurposeFlag(); // assert that extra data record is not present assertThat(BitUtils.isBitSet(generalPurposeBytes[0], 3)).isTrue(); }
Example 7
Source File: CollectEarthUtils.java From collect-earth with MIT License | 6 votes |
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 8
Source File: AddFilesToZipIT.java From zip4j with Apache License 2.0 | 5 votes |
@Test public void testAddStreamToZipThrowsExceptionWhenFileNameIsEmpty() throws IOException { ZipFile zipFile = new ZipFile(generatedZipFile); InputStream inputStream = new FileInputStream(TestUtils.getTestFileFromResources("бореиская.txt")); ZipParameters zipParameters = new ZipParameters(); zipParameters.setFileNameInZip(""); expectedException.expectMessage("fileNameInZip has to be set in zipParameters when adding stream"); expectedException.expect(ZipException.class); zipFile.addStream(inputStream, zipParameters); }
Example 9
Source File: AddFilesToZipIT.java From zip4j with Apache License 2.0 | 5 votes |
@Test public void testAddStreamToZipThrowsExceptionWhenFileNameIsNull() throws IOException { ZipFile zipFile = new ZipFile(generatedZipFile); InputStream inputStream = new FileInputStream(TestUtils.getTestFileFromResources("бореиская.txt")); ZipParameters zipParameters = new ZipParameters(); zipParameters.setFileNameInZip(null); expectedException.expectMessage("fileNameInZip has to be set in zipParameters when adding stream"); expectedException.expect(ZipException.class); zipFile.addStream(inputStream, zipParameters); }
Example 10
Source File: AddFilesToZipIT.java From zip4j with Apache License 2.0 | 5 votes |
@Test public void testAddFileWithDifferentFileNameSetsTheNewFileName() throws IOException { ZipFile zipFile = new ZipFile(generatedZipFile); ZipParameters zipParameters = new ZipParameters(); zipParameters.setFileNameInZip("/data/newfile.txt"); zipFile.addFile(TestUtils.getTestFileFromResources("sample_text_large.txt"), zipParameters); zipFile = new ZipFile(generatedZipFile); assertThat(zipFile.getFileHeaders()).hasSize(1); assertThat(zipFile.getFileHeader("/data/newfile.txt")).isNotNull(); assertThat(zipFile.getFileHeader("sample_text_large.txt")).isNull(); zipFile.extractAll(outputFolder.getPath()); }
Example 11
Source File: MiscZipFileIT.java From zip4j with Apache License 2.0 | 5 votes |
@Test public void testZipSlipFix() throws Exception { ZipParameters zipParameters = new ZipParameters(); zipParameters.setFileNameInZip("../../bad.txt"); ZipFile zip = new ZipFile(generatedZipFile); zip.addFile(TestUtils.getTestFileFromResources("sample_text1.txt"), zipParameters); try { zip.extractAll(outputFolder.getAbsolutePath()); fail("zip4j is vulnerable for slip zip"); } catch (ZipException e) { assertThat(e).hasMessageStartingWith("illegal file name that breaks out of the target directory: "); } }
Example 12
Source File: AbstractAddFileToZipTask.java From zip4j with Apache License 2.0 | 5 votes |
private ZipParameters cloneAndAdjustZipParameters(ZipParameters zipParameters, File fileToAdd, ProgressMonitor progressMonitor) throws IOException { ZipParameters clonedZipParameters = new ZipParameters(zipParameters); clonedZipParameters.setLastModifiedFileTime(javaToDosTime((fileToAdd.lastModified()))); if (fileToAdd.isDirectory()) { clonedZipParameters.setEntrySize(0); } else { clonedZipParameters.setEntrySize(fileToAdd.length()); } clonedZipParameters.setWriteExtendedLocalFileHeader(false); clonedZipParameters.setLastModifiedFileTime(fileToAdd.lastModified()); if (!Zip4jUtil.isStringNotNullAndNotEmpty(zipParameters.getFileNameInZip())) { String relativeFileName = getRelativeFileName(fileToAdd, zipParameters); clonedZipParameters.setFileNameInZip(relativeFileName); } if (fileToAdd.isDirectory()) { clonedZipParameters.setCompressionMethod(CompressionMethod.STORE); clonedZipParameters.setEncryptionMethod(EncryptionMethod.NONE); clonedZipParameters.setEncryptFiles(false); } else { if (clonedZipParameters.isEncryptFiles() && clonedZipParameters.getEncryptionMethod() == ZIP_STANDARD) { progressMonitor.setCurrentTask(CALCULATE_CRC); clonedZipParameters.setEntryCRC(computeFileCrc(fileToAdd, progressMonitor)); progressMonitor.setCurrentTask(ADD_ENTRY); } if (fileToAdd.length() == 0) { clonedZipParameters.setCompressionMethod(CompressionMethod.STORE); } } return clonedZipParameters; }
Example 13
Source File: ZipOutputStreamIT.java From zip4j with Apache License 2.0 | 5 votes |
private void testAddCommentToEntryWithCharset(Charset charset, String fileCommentPrefix) throws IOException { ZipParameters zipParameters = new ZipParameters(); byte[] buff = new byte[4096]; int readLen; List<File> filesToAdd = FILES_TO_ADD; try(ZipOutputStream zos = initializeZipOutputStream(false, charset)) { for (int i = 0; i < filesToAdd.size(); i++) { File fileToAdd = filesToAdd.get(i); zipParameters.setFileNameInZip(fileToAdd.getName()); if (i == 0) { zipParameters.setFileComment(fileCommentPrefix + i); } else { zipParameters.setFileComment(null); } zos.putNextEntry(zipParameters); try(InputStream inputStream = new FileInputStream(fileToAdd)) { while ((readLen = inputStream.read(buff)) != -1) { zos.write(buff, 0, readLen); } } zos.closeEntry(); } } verifyZipFileByExtractingAllFiles(generatedZipFile, PASSWORD, outputFolder, filesToAdd.size(), true, charset); verifyEntries(); verifyFileEntryComment(fileCommentPrefix, charset); }
Example 14
Source File: ZipOutputStreamIT.java From zip4j with Apache License 2.0 | 5 votes |
@Test public void testAddCommentToZipOutputStreamAfterClosingThrowsException() throws IOException { expectedException.expect(IOException.class); expectedException.expectMessage("Stream is closed"); ZipParameters zipParameters = new ZipParameters(); ZipOutputStream zos = initializeZipOutputStream(false, InternalZipConstants.CHARSET_UTF_8); for (File fileToAdd : FILES_TO_ADD) { zipParameters.setLastModifiedFileTime(fileToAdd.lastModified()); zipParameters.setFileNameInZip(fileToAdd.getName()); zos.putNextEntry(zipParameters); } zos.close(); zos.setComment("SOME_COMMENT"); }
Example 15
Source File: AddFilesToZipIT.java From zip4j with Apache License 2.0 | 5 votes |
@Test public void testAddStreamToZipWithAesEncryptionV2ForExistingZipAddsSuccessfully() throws IOException { File fileToAdd = TestUtils.getTestFileFromResources("가나다.abc"); ZipParameters zipParameters = createZipParameters(EncryptionMethod.AES, AesKeyStrength.KEY_STRENGTH_128); zipParameters.setFileNameInZip(fileToAdd.getName()); ZipFile zipFile = new ZipFile(generatedZipFile, PASSWORD); zipFile.addFiles(FILES_TO_ADD); InputStream inputStream = new FileInputStream(fileToAdd); zipFile.addStream(inputStream, zipParameters); ZipFileVerifier.verifyZipFileByExtractingAllFiles(generatedZipFile, PASSWORD, outputFolder, 4); verifyFileIsOf(generatedZipFile, "가나다.abc", CompressionMethod.DEFLATE, EncryptionMethod.AES, AesKeyStrength.KEY_STRENGTH_128, AesVersion.TWO); }
Example 16
Source File: FileUtilsTest.java From zip4j with Apache License 2.0 | 5 votes |
@Test public void testGetRelativeFileWithDifferentFileNameInZipAndRootFolderName() throws ZipException { String expectedRootFolder = "rootfolder" + FILE_SEPARATOR + "anotherName.txt"; ZipParameters zipParameters = new ZipParameters(); zipParameters.setFileNameInZip("anotherName.txt"); zipParameters.setRootFolderNameInZip("rootfolder"); assertThat(FileUtils.getRelativeFileName(new File("somefile.txt"), zipParameters)).isEqualTo(expectedRootFolder); }
Example 17
Source File: AddFilesToZipIT.java From zip4j with Apache License 2.0 | 5 votes |
@Test public void testAddStreamToZipWithCharsetCp949() throws IOException { String koreanFileName = "가나다.abc"; ZipFile zipFile = new ZipFile(generatedZipFile); File fileToAdd = TestUtils.getTestFileFromResources(koreanFileName); InputStream inputStream = new FileInputStream(fileToAdd); ZipParameters zipParameters = new ZipParameters(); zipParameters.setFileNameInZip(fileToAdd.getName()); zipFile.setCharset(CHARSET_CP_949); zipFile.addStream(inputStream, zipParameters); ZipFileVerifier.verifyZipFileByExtractingAllFiles(generatedZipFile, null, outputFolder, 1, true, CHARSET_CP_949); assertThat(zipFile.getFileHeaders().get(0).getFileName()).isEqualTo(koreanFileName); }
Example 18
Source File: AddFilesToZipIT.java From zip4j with Apache License 2.0 | 5 votes |
@Test public void testAddStreamToZipWithSameEntryNameRemovesOldEntry() throws IOException { File fileToAdd = TestUtils.getTestFileFromResources("sample.pdf"); ZipFile zipFile = new ZipFile(generatedZipFile); zipFile.addFile(fileToAdd); try(InputStream inputStream = new FileInputStream(fileToAdd)) { ZipParameters zipParameters = new ZipParameters(); zipParameters.setFileNameInZip("sample.pdf"); zipFile.addStream(inputStream, zipParameters); } ZipFileVerifier.verifyZipFileByExtractingAllFiles(generatedZipFile, outputFolder, 1); }
Example 19
Source File: MiscZipFileIT.java From zip4j with Apache License 2.0 | 5 votes |
@Test public void testZipSlipFixWithFileNameStartingWithParentDirectoryThrowsException() throws ZipException { ZipFile zipFile = new ZipFile(generatedZipFile); ZipParameters zipParameters = new ZipParameters(); zipParameters.setFileNameInZip("../somename.pdf"); zipFile.addFile(FILES_TO_ADD.get(0), zipParameters); expectedException.expectMessage("illegal file name that breaks out of the target directory"); expectedException.expect(ZipException.class); // Important here is that the name of the file in zip ("somename.pdf") should start with the // name of the directory being extracted to ("some"). "somename.pdf" starts with "some". zipFile.extractAll(outputFolder.getPath() + File.separator + "some"); }
Example 20
Source File: HeaderReaderIT.java From zip4j with Apache License 2.0 | 5 votes |
private List<FileHeader> generateFileHeaders(ZipParameters zipParameters, int numberOfEntries) throws ZipException { List<FileHeader> fileHeaders = new ArrayList<>(); for (int i = 0; i < numberOfEntries; i++) { zipParameters.setFileNameInZip(FILE_NAME_PREFIX + i); FileHeader fileHeader = fileHeaderFactory.generateFileHeader(zipParameters, false, 0, InternalZipConstants.CHARSET_UTF_8, rawIO); fileHeaders.add(fileHeader); } return fileHeaders; }