Java ZipおよびUnzipフォルダー

Aspose.Zip APIを使用すると、基礎となるファイル構造を心配することなく、ファイルを圧縮および解凍できます。この記事では、単一および複数のファイル圧縮を使用して作業していることを示しています。

ディレクトリの圧縮

ディレクトリコンテンツの圧縮

1try(fileoutputStream zipfile = new fileoutputStream(datadir + "compressdirectory_out.zip")){
2    try(archive archive = new archive()){
3        file corpus = new file(datadir + "canterburycorpus");
4        archive.createentries(corpus);
5        archive.save(zipfile);
6    }
7} catch(ioException ex){
8    System.out.println(ex);
9}

圧縮ディレクトリアーカイブの抽出

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}

ファイル情報によるファイルを圧縮

 1try(fileoutputStream zipfile = new fileoutputStream(datadir + "compressfilesbyfileinfo_out.zip")){
 2    ファイルfi1 = new file(datadir + "alice29.txt");
 3    ファイルfi2 = new file(datadir + "fields.c");
 4    try(archive archive = new archive()){
 5        archive.createentry( "alice29.txt"fi1);
 6        archive.createentry( "fields.c"fi2);
 7        ArchivesAveOptions options = new archivesaveoptions();
 8        options.SetEncoding(StandardCharsets.us_ascii);
 9        archive.save(zipfileoptions);
10    }
11} catch(ioException ex){
12    System.out.println(ex);
13}

異なるエントリアーカイブでアーカイブを抽出します

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_pa​​ss_out.txt" "first_pass");
4        archive.getEntries()get(1).extract(datadir + "asyoulik_extracted_pa​​ss_out.txt" "second_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.