Java Zip- und UnZip-Ordner

Mit der Aspose.ZIP API können Sie Dateien komprimieren und dekomprimieren, ohne sich um die zugrunde liegende Dateistruktur zu kümmern. Dieser Artikel zeigt, wie man sowohl mit einer als auch mit mehreren Dateien komprimiert.

Verzeichnis komprimieren

Verzeichnisinhalt komprimieren

1try (FileOutputStream zipFile = new FileOutputStream(dataDir + "CompressDirectory_out.zip")) {
2    try (Archiv archive = new Archiv()) {
3        Datei corpus = new File(dataDir + "CanterburyCorpus");
4        archive.createEntries(corpus);
5        archive.save(zipFile);
6    }
7} catch (IOException ex) {
8    System.out.println(ex);
9}

Komprimiertes Verzeichnisarchiv extrahieren

1try (FileInputStream zipFile = new FileInputStream(dataDir + "CompressDirectory_out.zip")) {
2    try (Archive archive = new Archive(zipFile)) {
3        archive.extractToDirectory(dataDir + "DecompressFolder_out");
4    }
5} catch (IOException ex) {
6    System.out.println(ex);
7}

Dateien nach Dateiinformationen komprimieren

 1try (FileOutputStream zipFile = new FileOutputStream(dataDir + "CompressFilesByFileInfo_out.zip")) {
 2    Datei fi1 = new Datei(dataDir + "alice29.txt");
 3    Datei fi2 = new File(dataDir + "fields.c");
 4    try (Archiv archive = new Archive()) {
 5        archive.createEntry("alice29.txt", fi1);
 6        archive.createEntry("felder.c", fi2);
 7        ArchiveSaveOptions options = new ArchiveSaveOptions();
 8        options.setEncoding(StandardCharsets.US_ASCII);
 9        archive.save(zipFile, options);
10    }
11} catch (IOException ex) {
12    System.out.println(ex);
13}

Archive mit unterschiedlichen Eingangsarchiven extrahieren

1try (FileInputStream zipFile = new FileInputStream(dataDir + "\\different_password.zip")) {
2    try (Archive archive = new Archive(zipFile)) {
3        archive.getEntries().get(0).extract(dataDir + "alice29_extracted_pass_out.txt", "first_pass");
4        archive.getEntries().get(1).extract(dataDir + "asyoulik_extrahierter_pass_out.txt", "zweiter_pass");
5    }
6} catch (IOException ex) {
7    System.out.println(ex);
8}
Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.