Javaでファイルをジップして解凍する方法
概要
この記事では、Java APIとサンプルコードを使用したさまざまな方法を使用して、ZIPファイルをプログラム的に圧縮する方法を教えてください。 1つ以上のファイルをジップまたは圧縮する方法と、同時にファイルをzipする方法を学びます。また、ファイルも解凍または解凍する方法を学びます。
Javaのファイルをzipまたは圧縮し、Javaのファイルを減圧または解凍します
Aspose.Zip APIを使用すると、基礎となるファイル構造を心配することなく、ファイルを圧縮および解凍できます。この記事では、単一および複数のファイル圧縮を使用して作業していることを示しています。
ファイルの圧縮
単一ファイルの圧縮
- ファイル出力ストリーム出力zipファイルの目的の名前を作成します。 2。 ファイル入力ストリームを作成して、圧縮するデータファイルを作成します。
- Archiveクラスのインスタンスを作成し、 ArchiveEntrySettingsクラスのインスタンスを渡します。
- archive.createentryメソッドを使用して、ステップ2で作成されたデータファイルを追加します。
- archive.saveメソッドを使用してデータファイルをzipし、ステップ1で作成されたファイルストリームを渡します。
1try(fileoutputStream zipfile = new fileoutputStream(datadir + "compresssinglefile_out.zip")){
2 //アーカイブに追加されるファイル
3 try(fileinputStream source1 = new fileinputStream(datadir + "alice29.txt")){
4 try(Archive archive = new Archive(new ArchiveEntrySettings())){
5 archive.createentry( "alice29.txt"、source1);
6 archive.save(zipfile);
7 }
8 }
9} catch(ioException ex){
10 System.out.println(ex);
11}
複数のファイルを圧縮する
1.出力zipファイルの目的の名前を持つファイルストリームを作成します。 2。圧縮される最初のデータファイルのファイルストリームを作成します。 3。圧縮される2番目のデータファイルのファイルストリームを作成します。 4. アーカイブクラスのインスタンスを作成します。 5. archive.createentryメソッドを使用して、ステップ2およびステップ3で作成されたデータファイルを追加します。 6. archivesaveoptionsクラスのインスタンスを作成します。 7. archive.saveメソッドを使用してデータファイルをzipし、上記のステップで作成されたarchivesaveoptionsのインスタンスで作成されたファイルストリームを渡します。
1try(fileoutputStream zipfile = new fileoutputStream(datadir + "compresssinglefile_out.zip")){
2 try(fileinputStream source1 = new fileinputStream(datadir + "alice29.txt")){
3 try(fileinputStream source2 = new fileinputStream(datadir + "asyoulik.txt")){
4 try(Archive archive = new Archive(new ArchiveEntrySettings())){
5 archive.createentry( "alice29.txt"、source1);
6 archive.createentry( "asyoulik.txt"、source2);
7 ArchivesAveOptions options = new archivesaveoptions();
8 options.SetEncoding(StandardCharsets.us_ascii);
9 options.setArchiveComment(「カンタベリーコーパスから2つの詩があります」);
10 archive.save(zipfile、options);
11 }
12 }
13 }
14} catch(ioException ex){
15 System.out.println(ex);
16}
ファイル情報によるファイルを圧縮
java ファイル情報でファイルを圧縮する 1.出力zipファイルの目的の名前を持つファイル出力ストリームを開きます。 2。最初のデータファイルの ファイルオブジェクトを作成して圧縮します。 3. ファイル 2番目のデータファイルのオブジェクトは、圧縮されます。 4. アーカイブクラスのインスタンスを作成します。 5. archive.createentryメソッドを使用して、ステップ2およびステップ3で作成されたデータファイルを追加します。 6. archivesaveoptionsクラスのインスタンスを作成します。 7. archive.saveメソッドを使用してデータファイルをzipし、上記のステップで作成されたarchivesaveoptionsのインスタンスで作成されたファイルストリームを渡します。
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(zipfile、options);
10 }
11} catch(IOExceptionが無視された){
12 System.out.println(ex);
13}
圧縮なしでファイルをアーカイブに保存
手順: javaを使用して圧縮なしでファイルをアーカイブに保存
1.出力zipファイルの目的の名前を持つファイル出力ストリームを開きます。 2。 ファイルデータファイルがアーカイブに保存されるオブジェクトを作成します。 3. ArchiveEntrySettingsクラスのインスタンスを作成し、 StoreCompressionsettingsクラスのインスタンスを渡します。 4. アーカイブクラスのインスタンスを作成し、上記のステップで作成されたarchiveentrysettingsクラスのインスタンスを渡します。 5。 archive.createentryメソッドを使用して、ステップ2で作成されたファイルを追加します。 6. archivesaveoptionsのインスタンスを作成し、 archivesaveoptions.setencodingメソッドを使用してstardandcharsets.us_asciiにエンコードを設定します。 7. archive.saveメソッドを使用してデータファイルをzipし、上記のステップで作成されたarchivesaveoptionsのインスタンスで作成されたファイルストリームを渡します。
1try(fileoutputStream zipfile = new fileoutputStream(datadir + "storemultiplefileswithoutcompression_out.zip"){
2 ファイルfi1 = new file(datadir + "alice29.txt");
3 ファイルfi2 = new file(datadir + "fields.c");
4 try(Archive Archive = new Archive(new ArchiveEntrySettings(new StoreCompressionsettings())){
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(zipfile、options);
10 }
11} catch(IOExceptionが無視された){
12 System.out.println(ex);
13}
並列処理を使用してファイルを圧縮する
ステップ:並列処理を使用してJavaを使用してファイルを圧縮する
1.出力zipファイルの目的の名前を持つファイル出力ストリームを開きます。 2。最初のデータファイルと2番目のデータファイルが圧縮されるためのファイル入力ストリームを開きます。 3. アーカイブクラスのインスタンスを作成します。 4. archive.createentryメソッドを使用して、ステップ2で作成されたデータファイルを追加します。 5. PARLELALELOPTIONSのインスタンスを作成し、 Paralleloptions.setParallecompressInmemoryメソッドを使用して、[ParallecompressionMode.always を設定します。 6. archivesaveoptionsのインスタンスを作成し、 archivesaveoptions.setparalleloptionsメソッドを使用して上記のインスタンスで並列オプションを設定します。 7. archive.saveメソッドを使用してデータファイルをzipし、上記のステップで作成されたarchivesaveoptions**のインスタンスで作成されたファイルストリームを渡します。
1try(fileoutputStream zipfile = new fileoutputStream(datadir + "を使用してParallelismTocopressfiles_out.zip"){
2 try(fileinputStream source1 = new fileinputStream(datadir + "alice29.txt")){
3 try(fileinputStream source2 = new fileinputStream(datadir + "asyoulik.txt")){
4 try(Archive archive = new Archive(new ArchiveEntrySettings())){
5 archive.createentry( "alice29.txt"、source1);
6 archive.createentry( "asyoulik.txt"、source2);
7 Paralleloptions parlalEloptions = new Paralleloptions();
8 paralleloptions.setParallecompressinmemory(parallecompressionMode.always);
9 ArchivesAveOptions options = new archivesaveoptions();
10 options.setParalleloptions(Paralleloptions);
11 options.SetEncoding(StandardCharsets.us_ascii);
12 options.setArchiveComment(「カンタベリーコーパスから2つの詩があります」);
13 archive.save(zipfile、options);
14 }
15 }
16 }
17} catch(ioException ex){
18 System.out.println(ex);
19}
zipアーカイブ内のLZMA圧縮
lempel – ziv – markovチェーンアルゴリズム(lzma)は、ロスレスデータ圧縮を実行するために使用されるアルゴリズムです。 LZMAは辞書圧縮アルゴリズムを使用し、圧縮ストリームはビットのストリームです。 ZIPアーカイブ内のLZMA圧縮により、ZIPコンテナはLZMA圧縮エントリを含めることができます。次のコードの例は、Aspose.Zip APIを使用したLZMA圧縮の実装を示しています。
手順:java を使用したzipアーカイブ内のLZMA圧縮
1.出力zipファイルの目的の名前を持つファイル出力ストリームを開きます。 2。 archiveentrysettingsクラスのインスタンスを作成し、 lzmacompressionsettingsクラスのインスタンスを渡します。 3. アーカイブクラスのインスタンスを作成し、上記で作成したArchiveEntrySettingsのインスタンスを渡します。 4. archive.createentryメソッドを使用して、ファイルパスを介して圧縮されるデータファイルを追加します。 5。 archive.saveメソッドを使用してデータファイルをzipします。
1try(fileoutputStream zipfile = new fileoutputStream(datadir + "lzmacompression_out.zip")){
2 try(Archive Archive = new Archive(new ArchiveEntrySettings(new lzmacompressionsettings())){
3 archive.createentry( "sample.txt"、datadir + "sample.txt");
4 archive.save(zipfile);
5 }
6} catch(IOExceptionが無視された){
7 System.out.println(ex);
8}
zipアーカイブ内のbzip2圧縮
BZIP2圧縮設定により、ZIPコンテナはBZIP2圧縮エントリを含めることができます。 次のコードの例は、Aspose.Zip APIを使用したBZIP2圧縮の実装を示しています。
手順:java を使用したzipアーカイブ内のzipアーカイブ内のbzip2圧縮
1.出力zipファイルの目的の名前を持つファイル出力ストリームを開きます。 2。 archiveentrysettingsクラスのインスタンスを作成し、 bzip2compressionsettingsクラスのインスタンスを渡します。 4. アーカイブクラスのインスタンスを作成し、上記で作成したarchiveentrysettingsのインスタンスを渡します。 5. archive.createentryメソッドを使用して、ファイルパスを介して圧縮されるデータファイルを追加します。 6. archive.saveメソッドを使用してデータファイルをzipします。
1try(fileoutputStream zipfile = new fileoutputStream(datadir + "bzip2compression_out.zip")){
2 try(Archive Archive = new Archive(new ArchiveEntrySettings(new bzip2Compressionsettings())){
3 archive.createentry( "sample.txt"、datadir + "sample.txt");
4 archive.save(zipfile);
5 }
6} catch(IOExceptionが無視された){
7 System.out.println(ex);
8}
アーカイブの減圧
単一のファイルを持つアーカイブを解凍
1try(fileinputStream fs = new fileInputStream(datadir + "compressSinglefile_out.zip")){
2 try(Archive Archive = new Archive(fs)){
3 int []パーセントready = new int [] {0};
4 archive.getEntries()。get(0).setExtractionProgressed(new event <prowresventargs>(){
5 @オーバーライド
6 public void invoke(Object sender、progresseventargs progresseventargs){
7 intパーセント=(int)((100 * progresseventargs.getproceedbytes()))
8 /((ArchiveEntry)送信者).getunCompressedSize());
9 if(パーセント>パーセント準備[0])
10 {
11 System.out.println(パーセント + "%減圧");
12 PercentReady [0] =パーセント;
13 }
14 }
15 });
16 archive.getentries()。get(0).extrage(datadir + "alice_extracted_out.txt");
17 }
18} catch(ioException ex){
19 System.out.println(ex);
20}
複数のファイルを持つアーカイブを解凍
1try(fileInputStream fs = new fileInputStream(datadir + "compressMultipleFiles_out.zip")){
2 stringbuilder sb = new StringBuilder( "エントリは:");
3 int []パーセントready = new int [] {0};
4 archiveloadoptions options = new archiveloadoptions();
5 options.setEntryListed(new event <Entereventargs>(){
6 @オーバーライド
7 public void invoke(Object sender、entereventargs entereventargs){
8 sb.append(entereventargs.getentry()。getname())。append( "、");
9 }
10 });
11 options.setEntryExtractionProgressed(新しいイベント<ProgressEventargs>(){
12 @オーバーライド
13 public void invoke(Object sender、progresseventargs progresseventargs){
14 intパーセント=(int)((100 * progresseventargs.getproceedbytes()))
15 /((ArchiveEntry)送信者).getunCompressedSize());
16 if(パーセント>パーセント準備[0])
17 {
18 System.out.println(パーセント + "%圧縮");
19 PercentReady [0] =パーセント;
20 }
21 }
22 });
23 try(Archive Archive = new Archive(fs、options)){
24 System.out.println(sb.substring(0、sb.length()-2));
25 try(fileoutputStream抽出= new fileoutputStream(datadir + "alice_extracted_out.txt")){
26 try(inputstream decompressed = archive.getentries()。get(0).open()){
27 byte [] buffer = new byte [8192];
28 int bytesRead;
29 while(0 <(bytesread = decompressed.read(buffer、0、buffer.length))){
30 抽出された.write(buffer、0、bytesread);
31 }
32 //解凍ストリームからファイルを抽出するまで読み取ります。
33 }
34 }
35 パーセントReady [0] = 0;
36 archive.getEntries()。get(1).extract(datadir + "asyoulik_extracted_out.txt");
37 }
38} catch(ioException ex){
39 System.out.println(ex);
40}
圧縮なしで保存されたアーカイブを抽出します
1try(fileinputStream zipfile = new fileinputStream(datadir + "storemultiplefileswithoutcompression_out.zip"){
2 try(Archive archive = new Archive(zipfile))){
3 try(fileoutputStream抽出= new fileoutputStream(datadir + "alice_extracted_store_out.txt")){
4 try(inputstream stored = archive.getentries()。get(0).open()){
5 byte [] buffer = new byte [8192];
6 int bytesRead;
7 while(0 <(bytesread = stored.read(buffer、0、buffer.length))){
8 抽出された.write(buffer、0、bytesread);
9 }
10 //保存されたストリームからファイルを抽出するまで読み取ります。
11 }
12 }
13 try(fileoutputStream抽出= new fileoutputStream(datadir + "asyoulik_extracted_store_out.txt")){
14 try(inputstream stored = archive.getentries()。get(1).open()){
15 byte [] buffer = new byte [8192];
16 int bytesRead;
17 while(0 <(bytesread = stored.read(buffer、0、buffer.length))){
18 抽出された.write(buffer、0、bytesread);
19 }
20 //保存されたストリームからファイルを抽出するまで読み取ります。
21 }
22 }
23 }
24} catch(ioException ex){
25 System.out.println(ex);
26}