パスワード保護アーカイブ

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

アーカイブの圧縮

従来の暗号化スキームでファイルを暗号化

1try(fileoutputStream zipfile = new fileoutputStream(datadir + "compresswithtraditionalencryption_out.zip"){
2    try(fileinputStream source1 = new fileinputStream(datadir + "alice29.txt")){
3        Archive Archive = new Archive(新しいArchiveEntrySettings(nullnew TraditionalEncryptionsettings( "p@s $")));
4        archive.createentry( "alice29.txt"source1);
5        archive.save(zipfile);
6    }
7} catch(ioException ex){
8    System.out.println(ex);
9}

AES暗号化を備えたファイルを暗号化

AES128を使用したファイルの暗号化

1try(fileoutputStream zipfile = new fileoutputStream(datadir + "passwordprotectwithaes128_out.zip"){
2    try(fileinputStream source1 = new fileinputStream(datadir + "alice29.txt")){
3        Archive Archive = new Archive(新しいArchiveEntrySettings(nullnew aesencryptionsettings( "p@s $"encryptionmethod.aes128)));
4        archive.createentry( "alice29.txt"source1);
5        archive.save(zipfile);
6    }
7} catch(ioException ex){
8    System.out.println(ex);
9}

AES192を使用したファイルの暗号化

1try(fileoutputStream zipfile = new fileoutputStream(datadir + "passwordProtectWithAes192_out.zip"){
2    try(fileinputStream source1 = new fileinputStream(datadir + "alice29.txt")){
3        Archive Archive = new Archive(新しいArchiveEntrySettings(nullnew aesencryptionsettings( "p@s $"encryptionmethod.aes192)));
4        archive.createentry( "alice29.txt"source1);
5        archive.save(zipfile);
6    }
7} catch(ioException ex){
8    System.out.println(ex);
9}

AES256を使用したファイルの暗号化

1try(fileoutputStream zipfile = new fileoutputStream(datadir + "passwordprotectwithaes256_out.zip"){
2    try(fileinputStream source1 = new fileinputStream(datadir + "alice29.txt")){
3        Archive Archive = new Archive(新しいArchiveEntrySettings(nullnew aesencryptionsettings( "p@s $"encryptionmethod.aes256)));
4        archive.createentry( "alice29.txt"source1);
5        archive.save(zipfile);
6    }
7} catch(ioException ex){
8    System.out.println(ex);
9}

パスワード保護ディレクトリ

1try(fileoutputStream zipfile = new fileoutputStream( "。\\ all_corpus_encrypted_out.zip"){
2    file corpus = new file( "。\\ canterburycorpus");
3    try(Archive Archive = new Archive(新しいArchiveEntrySettings(nullnew TraditionalEncryptionsettings( "p@s $"))){
4        archive.createentries(corpus);
5        archive.save(zipfile);
6    }
7} catch(ioException ex){
8    System.out.println(ex);
9}

複数のファイルを混合した暗号化テクニックで暗号化

 1try(fileoutputStream zipfile = new fileoutputStream( "archive.zip")){
 2    ファイルfi1 = new file( "data1.bin");
 3    ファイルfi2 = new file( "data2.bin");
 4    ファイルfi3 = new file( "data3.bin");
 5    try(archive archive = new archive()){
 6        archive.createentry( "entry1.bin"、​​fi1falsenew archiveentrysettings(new deflateCompressionsettings()新しいTraditionalEncryptionsettings( "pass1")));
 7        archive.createentry( "entry2.bin"、​​fi2falsenew archiveEntrysettings(new deflateCompressionsettings()new aesencryptionsettings( "pass2"encryptionmethod.aes128)));
 8        archive.createentry( "entry3.bin"、​​fi3falsenew archiveentrysettings(new deflateCompressionsettings()new aesencryptionsettings( "pass3"encryptionmethod.aes256)));
 9        archive.save(zipfile);
10    }
11} catch(IOExceptionが無視された){
12}

アーカイブの減圧

伝統的にパスワード保護されたアーカイブの減圧

 1try(fileinputStream fs = new fileinputStream(datadir + "compresswithtraditionalencryption_in.zip")){
 2    try(fileoutputStream抽出= new fileoutputStream(datadir + "alice_extracted_out.txt")){
 3        archiveloadoptions options = new archiveloadoptions();
 4        options.setDecryptionPassWord( "p@s $");
 5        try(Archive Archive = new Archive(fsoptions)){
 6            try(inputstream decompressed = archive.getentries()get(0).open()){
 7                byte [] b = new byte [8192];
 8                int bytesRead;
 9                while(0 <(bytesread = decompressed.read(b0b.length))){
10                    抽出された.write(b0bytesread);
11                }
12            }
13        }
14    }
15} catch(ioException ex){
16    System.out.println(ex);
17}

AES暗号化されたアーカイブを減圧します

 1try(fileInputStream fs = new fileInputStream(datadir + "passwordprotectwithaes256_in.zip")){
 2    try(fileoutputStream抽出= new fileoutputStream(datadir + "alice_aesextracted_out.txt")){
 3        try(Archive Archive = new Archive(fs)){
 4            try(inputstream decompressed = archive.getentries()get(0).open( "p@s $")){
 5                byte [] b = new byte [8192];
 6                int bytesRead;
 7                while(0 <(bytesread = decompressed.read(b0b.length))){
 8                    抽出された.write(b0bytesread);
 9                }
10            }
11        }
12    }
13} catch(ioException ex){
14    System.out.println(ex);
15}

AES暗号化された保存されたアーカイブを減圧します

 1try(fileInputStream fs = new fileInputStream(datadir + "storemutliplefileswithcompressionwithpassword_in.zip"){
 2    try(fileoutputStream抽出= new fileoutputStream(datadir + "alice_aesextracted_out.txt")){
 3        archiveloadoptions options = new archiveloadoptions();
 4        options.setDecryptionPassWord( "p@s $");
 5        try(Archive Archive = new Archive(fsoptions)){
 6            try(inputstream decompressed = archive.getentries()get(0).open()){
 7                byte [] b = new byte [8192];
 8                int bytesRead;
 9                while(0 <(bytesread = decompressed.read(b0b.length))){
10                    抽出された.write(b0bytesread);
11                }
12            }
13        }
14    }
15} catch(ioException ex){
16    System.out.println(ex);
17}

暗号化されたフォルダーをディレクトリに減圧します

1try(fileinputStream zipfile = new fileinputStream( "。\\ all_corpus_encrypted.zip")){
2    archiveloadoptions options = new archiveloadoptions();
3    options.setDecryptionPassWord( "p@s $");
4    新しいアーカイブ(zipfileoptions).extracttodirectory( "。\\ all_corpus_decrypted");
5} catch(ioException ex){
6    System.out.println(ex);
7}

単一のファイルを持つアーカイブを解凍

 1try(fileInputStream fs = new fileInputStream(datadir + "compressedsinglefile.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 senderprogresseventargs progresseventargs){
 7                intパーセント=(int)((100 * progresseventargs.getProceedBytes()) /((archiveentry)sender).getuncompressedsize());
 8                if(パーセント>パーセントReady [0]){
 9                    System.out.println(パーセント + "%減圧");
10                    PercentReady [0] =パーセント;
11                }
12            }
13        });
14        archive.getentries()get(0).extrage(datadir + "alice_extracted_out.txt");
15    }
16} catch(ioException ex){
17    System.out.println(ex);
18}

複数のファイルを持つアーカイブを解凍

 1try(fileInputStream fs = new fileinputStream(datadir + "compressedmultiplefiles.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 senderentereventargs entereventargs){
 8            sb.append(entereventargs.getentry()getname())append( "、");
 9        }
10    });
11    options.setEntryExtractionProgressed(新しいイベント<ProgressEventargs>(){
12        @オーバーライド
13        public void invoke(Object senderprogresseventargs progresseventargs){
14            intパーセント=(int)((100 * progresseventargs.getProceedBytes()) /((archiveentry)sender).getuncompressedsize());
15            if(パーセント>パーセントReady [0]){
16                System.out.println(パーセント + "%圧縮");
17                PercentReady [0] =パーセント;
18            }
19        }
20    });
21    try(Archive Archive = new Archive(fsoptions)){
22        System.out.println(sb.substring(0sb.length()-2));
23        try(fileoutputStream抽出= new fileoutputStream(datadir + "alice_extracted_out.txt")){
24            try(inputstream decompressed = archive.getentries()get(0).open()){
25                byte [] buffer = new byte [8192];
26                int bytesRead;
27                while(0 <(bytesread = decompressed.read(buffer0buffer.length))){
28                    抽出された.write(buffer0bytesread);
29                }
30                //解凍ストリームからファイルを抽出するまで読み取ります。
31            }
32        }
33        パーセントReady [0] = 0;
34        archive.getEntries()get(1).extract(datadir + "asyoulik_extracted_out.txt");
35    }
36} catch(ioException ex){
37    System.out.println(ex);
38}

圧縮なしで保存されたアーカイブを抽出します

 1try(fileInputStream zipfile = new fileinputStream(datadir + "storemultiplefileswithoutcompression.zip"){
 2    try(Archive archive = new Archive(zipfile))){
 3        try(fileoutputStream抽出= new fileoutputStream(datadir + "alice_extracted_store_out.txt")){
 4            try(inputstream decompressed = archive.getentries()get(0).open()){
 5                byte [] buffer = new byte [8192];
 6                int bytesRead;
 7                while(0 <(bytesread = decompressed.read(buffer0buffer.length))){
 8                    抽出された.write(buffer0bytesread);
 9                }
10                //解凍ストリームからファイルを抽出するまで読み取ります。
11            }
12        }
13        try(fileoutputStream抽出= new fileoutputStream(datadir + "asyoulik_extracted_store_out.txt")){
14            try(inputstream decompressed = archive.getentries()get(1).open()){
15                byte [] buffer = new byte [8192];
16                int bytesRead;
17                while(0 <(bytesread = decompressed.read(buffer0buffer.length))){
18                    抽出された.write(buffer0bytesread);
19                }
20                //解凍ストリームからファイルを抽出するまで読み取ります。
21            }
22        }
23    }
24} catch(ioException ex){
25    System.out.println(ex);
26}
Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.