Java Code Examples for net.lingala.zip4j.model.ZipParameters#setIncludeRootFolder()

The following examples show how to use net.lingala.zip4j.model.ZipParameters#setIncludeRootFolder() . 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: CreateZipFileIT.java    From zip4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testAddSymlinksInAFolderWithLinkAndLinkedFilesOnlyWithoutRootFolder() throws IOException {
  File testFolder = createTestFolderWithSymlinks();
  ZipFile zipFile = new ZipFile(generatedZipFile);
  ZipParameters zipParameters = new ZipParameters();
  zipParameters.setIncludeRootFolder(false);
  zipParameters.setSymbolicLinkAction(ZipParameters.SymbolicLinkAction.INCLUDE_LINK_AND_LINKED_FILE);

  zipFile.addFolder(testFolder, zipParameters);

  verifyZipFileByExtractingAllFiles(generatedZipFile, null, outputFolder, 8, false);
  verifyFileNamesInZip(zipFile,
      "symlink.link",
      "sample.pdf",
      "sub-folder1/",
      "sub-folder1/symlink.link",
      "sub-folder1/file_PDF_1MB.pdf",
      "sub-folder2/",
      "sub-folder2/sample_text_large.txt",
      "sub-folder2/symlink.link");
}
 
Example 2
Source File: UserModelService.java    From KOMORAN with Apache License 2.0 6 votes vote down vote up
public File deployUserModel(String modelDir) {
    ModelValidator.CheckValidModelName(modelDir);

    String dirNameToArchive = String.join(File.separator, MODELS_BASEDIR, modelDir);
    String zipNameToArchive = "UserModel" + modelDir + ".zip";

    ZipFile zipFileToDeploy = new ZipFile(zipNameToArchive);
    ZipParameters zipFileParams = new ZipParameters();

    zipFileParams.setReadHiddenFiles(false);
    zipFileParams.setReadHiddenFolders(false);
    zipFileParams.setIncludeRootFolder(false);

    try {
        zipFileToDeploy.addFolder(new File(dirNameToArchive), zipFileParams);
    } catch (ZipException e) {
        throw new ServerErrorException("모델 배포를 위한 압축 파일 생성에 실패했습니다.");
    }

    return zipFileToDeploy.getFile();
}
 
Example 3
Source File: GenerateFinalZip.java    From apk-decompiler with GNU General Public License v2.0 6 votes vote down vote up
private void generateZipFile() {
	try {
		File outputFile = new File(this.workFolder.getName() + ".zip");
		if (outputFile.exists()) {
			Logger.info(outputFile.getAbsolutePath() + " already exists. Deleting.");
			outputFile.delete();
		}
		ZipFile output = new net.lingala.zip4j.core.ZipFile(outputFile);
		Logger.info("Generating " + output.getFile().getAbsolutePath());

		ZipParameters params = new ZipParameters();
		params.setIncludeRootFolder(false);
		params.setRootFolderInZip("");
		output.addFolder(this.workFolder.getAbsolutePath() + File.separator + "apktool", params);
		params.setRootFolderInZip("classes");
		output.addFolder(this.workFolder.getAbsolutePath() + File.separator + "classes", params);
	} catch (Throwable t) {
		Logger.error("Unable to generate final zip file.", t);
	}
}
 
Example 4
Source File: AddFilesToZipIT.java    From zip4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testAddFolderWithExcludeFileFilter() throws IOException {
  ZipFile zipFile = new ZipFile(generatedZipFile);
  List<File> filesToExclude = Arrays.asList(
      TestUtils.getTestFileFromResources("sample.pdf"),
      TestUtils.getTestFileFromResources("sample_directory/favicon.ico")
  );
  ZipParameters zipParameters = new ZipParameters();
  zipParameters.setIncludeRootFolder(false);
  zipParameters.setExcludeFileFilter(filesToExclude::contains);

  zipFile.addFolder(TestUtils.getTestFileFromResources(""), zipParameters);

  ZipFileVerifier.verifyZipFileByExtractingAllFiles(generatedZipFile, outputFolder, 10);
  verifyZipFileDoesNotContainFiles(generatedZipFile, Arrays.asList("sample.pdf", "sample_directory/favicon.ico"));
}
 
Example 5
Source File: GenerateFinalZip.java    From apk-decompiler with GNU General Public License v2.0 6 votes vote down vote up
private void generateZipFile() {
	try {
		File outputFile = new File(this.workFolder.getName() + ".zip");
		if (outputFile.exists()) {
			Logger.info(outputFile.getAbsolutePath() + " already exists. Deleting.");
			outputFile.delete();
		}
		ZipFile output = new net.lingala.zip4j.core.ZipFile(outputFile);
		Logger.info("Generating " + output.getFile().getAbsolutePath());

		ZipParameters params = new ZipParameters();
		params.setIncludeRootFolder(false);
		params.setRootFolderInZip("");
		output.addFolder(this.workFolder.getAbsolutePath() + File.separator + "apktool", params);
		params.setRootFolderInZip("classes");
		output.addFolder(this.workFolder.getAbsolutePath() + File.separator + "classes", params);
	} catch (Throwable t) {
		Logger.error("Unable to generate final zip file.", t);
	}
}
 
Example 6
Source File: CejshBuilder.java    From proarc with GNU General Public License v3.0 5 votes vote down vote up
void writeZip(File pkgFile, File packageFolder) throws ZipException {
    ZipFile zipFile = new ZipFile(pkgFile);
    ZipParameters zipParameters = new ZipParameters();
    zipParameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD);
    zipParameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
    zipParameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
    zipParameters.setIncludeRootFolder(false);
    zipParameters.setDefaultFolderPath(packageFolder.getAbsolutePath());
    zipFile.addFiles(listZipFiles(packageFolder), zipParameters);
}
 
Example 7
Source File: AppPackage.java    From Bats with Apache License 2.0 5 votes vote down vote up
public static void createAppPackageFile(File fileToBeCreated, File directory) throws ZipException
{
  ZipFile zipFile = new ZipFile(fileToBeCreated);
  ZipParameters params = new ZipParameters();
  params.setIncludeRootFolder(false);
  zipFile.addFolder(directory, params);
}
 
Example 8
Source File: StramTestSupport.java    From attic-apex-core with Apache License 2.0 5 votes vote down vote up
/**
 * Create an confPackage zip using the sample confPackage located in
 * src/test/resources/testConfPackage/testConfPackageSrc.
 *
 * @param file The file whose path will be used to create the confPackage zip
 * @return The File object that can be used in the ConfigPackage constructor.
 * @throws net.lingala.zip4j.exception.ZipException
 */
public static File createConfigPackageFile(File file) throws net.lingala.zip4j.exception.ZipException
{
  ZipFile zipFile = new ZipFile(file);
  ZipParameters zipParameters = new ZipParameters();
  zipParameters.setIncludeRootFolder(false);
  zipFile.createZipFileFromFolder("src/test/resources/testConfigPackage/testConfigPackageSrc", zipParameters, false, Long.MAX_VALUE);
  return file;
}
 
Example 9
Source File: AppPackage.java    From attic-apex-core with Apache License 2.0 5 votes vote down vote up
public static void createAppPackageFile(File fileToBeCreated, File directory) throws ZipException
{
  ZipFile zipFile = new ZipFile(fileToBeCreated);
  ZipParameters params = new ZipParameters();
  params.setIncludeRootFolder(false);
  zipFile.addFolder(directory, params);
}
 
Example 10
Source File: ZipUtil.java    From VoxelGamesLibv2 with MIT License 5 votes vote down vote up
/**
 * Creates a zip from all files and folders in the specified folder. DOES NOT INCLUDE THE FOLDER ITSELF!
 *
 * @param file the folder which content should be zipped
 * @return the created zip
 * @throws ZipException if something goes wrong
 */
@Nonnull
public static ZipFile createZip(@Nonnull File file, @Nonnull String name) throws ZipException {
    ZipFile zip = new ZipFile(new File(file.getParent(), name + ".zip"));

    ZipParameters params = new ZipParameters();
    params.setIncludeRootFolder(false);
    zip.addFolder(file, params);

    return zip;
}
 
Example 11
Source File: FileIO.java    From PHONK with GNU General Public License v3.0 5 votes vote down vote up
static public void zipFolder(String src, String dst) throws Exception {
    File f = new File(dst);
    //make dirs if necessary
    f.getParentFile().mkdirs();

    ZipFile zipfile = new ZipFile(f.getAbsolutePath());
    ZipParameters parameters = new ZipParameters();
    parameters.setIncludeRootFolder(false);
    parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
    parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
    zipfile.addFolder(src, parameters);
}
 
Example 12
Source File: ZipPack.java    From ResourcePackConverter with MIT License 5 votes vote down vote up
@Override
public void finish() throws IOException {
    try {
        System.out.println("  Zipping working directory");
        ZipFile zipFile = new ZipFile(getConvertedZipPath().toFile());
        ZipParameters parameters = new ZipParameters();
        parameters.setIncludeRootFolder(false);
        zipFile.createZipFileFromFolder(pack.getWorkingPath().toFile(), parameters, false, 65536);
    } catch (ZipException e) {
        Util.propagate(e);
    }

    System.out.println("  Deleting working directory");
    Util.deleteDirectoryAndContents(pack.getWorkingPath());
}
 
Example 13
Source File: AddFilesToZipIT.java    From zip4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testAddFolderWithRootFolderNameInZipAndWithRootFolder() throws IOException {
  ZipParameters zipParameters = createZipParameters(EncryptionMethod.AES, AesKeyStrength.KEY_STRENGTH_256);
  zipParameters.setIncludeRootFolder(true);
  zipParameters.setRootFolderNameInZip("root_folder_name");
  ZipFile zipFile = new ZipFile(generatedZipFile, PASSWORD);

  zipFile.addFolder(TestUtils.getTestFileFromResources(""), zipParameters);

  List<FileHeader> fileHeaders = getFileHeaders(generatedZipFile);
  verifyAllFilesInZipContainsPath(fileHeaders, "root_folder_name/");
  verifyAllFilesInZipContainsPath(fileHeaders, "root_folder_name/test-files/");
  verifyFoldersInZip(fileHeaders, generatedZipFile, PASSWORD);
}
 
Example 14
Source File: AddFilesToZipIT.java    From zip4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testAddFolderWithRootFolderNameInZipAndWithoutRootFolder() throws IOException {
  ZipParameters zipParameters = createZipParameters(EncryptionMethod.AES, AesKeyStrength.KEY_STRENGTH_256);
  zipParameters.setIncludeRootFolder(false);
  zipParameters.setRootFolderNameInZip("root_folder_name");
  ZipFile zipFile = new ZipFile(generatedZipFile, PASSWORD);

  zipFile.addFolder(TestUtils.getTestFileFromResources(""), zipParameters);

  List<FileHeader> fileHeaders = getFileHeaders(generatedZipFile);
  verifyAllFilesInZipContainsPath(fileHeaders, "root_folder_name/");
  verifyAllFilesInZipDoesNotContainPath(fileHeaders, "root_folder_name/test-files/");
  verifyFoldersInZip(fileHeaders, generatedZipFile, PASSWORD);
}
 
Example 15
Source File: AddFilesToZipIT.java    From zip4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testAddFolderWithDeflateAndAes256AndWithoutRootFolder() throws IOException {
  ZipParameters zipParameters = createZipParameters(EncryptionMethod.AES, AesKeyStrength.KEY_STRENGTH_256);
  zipParameters.setIncludeRootFolder(false);
  ZipFile zipFile = new ZipFile(generatedZipFile, PASSWORD);

  zipFile.addFolder(TestUtils.getTestFileFromResources(""), zipParameters);

  ZipFileVerifier.verifyZipFileByExtractingAllFiles(generatedZipFile, PASSWORD, outputFolder, 12);
  List<FileHeader> fileHeaders = getFileHeaders(generatedZipFile);
  verifyAllFilesInZipDoesNotContainPath(fileHeaders, "test-files/");
  verifyFoldersInZip(fileHeaders, generatedZipFile, PASSWORD);
}
 
Example 16
Source File: ExtractZipFileIT.java    From zip4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testExtractFileHeaderExtractAllFilesIfFileHeaderIsDirectoryAndRenameFile() throws IOException {
  ZipFile zipFile = new ZipFile(generatedZipFile);
  ZipParameters zipParameters = new ZipParameters();
  zipParameters.setIncludeRootFolder(false);
  zipFile.addFolder(TestUtils.getTestFileFromResources(""), zipParameters);

  zipFile.extractFile(zipFile.getFileHeader("öüäöäö/"), outputFolder.getPath(), "new_folder_name/");
  File outputFile = Paths.get(outputFolder.getPath(), "new_folder_name", "asöäööl").toFile();
  assertThat(outputFile).exists();
  ZipFileVerifier.verifyFileContent(TestUtils.getTestFileFromResources("öüäöäö/asöäööl"), outputFile);
}
 
Example 17
Source File: ExtractZipFileIT.java    From zip4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testExtractFileHeaderExtractAllFilesIfFileHeaderIsDirectory() throws IOException {
  ZipFile zipFile = new ZipFile(generatedZipFile);
  ZipParameters zipParameters = new ZipParameters();
  zipParameters.setIncludeRootFolder(false);
  zipFile.addFolder(TestUtils.getTestFileFromResources(""), zipParameters);

  zipFile.extractFile(zipFile.getFileHeader("öüäöäö/"), outputFolder.getPath());
  File outputFile = Paths.get(outputFolder.getPath(), "öüäöäö", "asöäööl").toFile();
  assertThat(outputFile).exists();
  ZipFileVerifier.verifyFileContent(TestUtils.getTestFileFromResources("öüäöäö/asöäööl"), outputFile);
}
 
Example 18
Source File: RenameFilesInZipIT.java    From zip4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testRenameWithMapProgressMonitor() throws IOException, InterruptedException {
  TestUtils.copyFileToFolder(getTestFileFromResources("file_PDF_1MB.pdf"), temporaryFolder.getRoot(), 100);

  ZipFile zipFile = new ZipFile(generatedZipFile, PASSWORD);
  ZipParameters zipParameters = buildZipParameters(CompressionMethod.DEFLATE, true, EncryptionMethod.AES);
  zipParameters.setIncludeRootFolder(false);
  zipFile.addFolder(temporaryFolder.getRoot(), zipParameters);

  Map<String, String> fileNamesMap = new HashMap<>();
  fileNamesMap.put("1.pdf", "1_new.pdf");
  fileNamesMap.put("25.pdf", "25_new.pdf");

  zipFile.setRunInThread(true);
  ProgressMonitor progressMonitor = zipFile.getProgressMonitor();

  zipFile.renameFiles(fileNamesMap);

  boolean percentBetweenZeroAndHundred = false;
  boolean fileNameSet = true;
  while (!progressMonitor.getState().equals(ProgressMonitor.State.READY)) {
    int percentDone = progressMonitor.getPercentDone();
    String fileName = progressMonitor.getFileName();

    if (percentDone > 0 && percentDone < 100) {
      percentBetweenZeroAndHundred = true;
    }

    if (fileName != null) {
      fileNameSet = true;
    }

    Thread.sleep(10);
  }

  assertThat(progressMonitor.getResult()).isEqualTo(ProgressMonitor.Result.SUCCESS);
  assertThat(progressMonitor.getState().equals(ProgressMonitor.State.READY));
  assertThat(progressMonitor.getException()).isNull();
  assertThat(percentBetweenZeroAndHundred).isTrue();
  assertThat(fileNameSet).isTrue();

  ZipFileVerifier.verifyZipFileByExtractingAllFiles(generatedZipFile, PASSWORD, outputFolder, 100, false);
  verifyFileNamesChanged(zipFile, fileNamesMap, false);
}