Extended zip4j memory encryption compressed package

Source: Internet
Author: User
Tags unsupported

Import java. io. byteArrayOutputStream; import java. io. fileOutputStream; import java. io. IOException; import net.lingala.zip 4j. exception. zipException; import net.lingala.zip 4j. io. zipOutputStream; import net.lingala.zip 4j. model. zipModel; import net.lingala.zip 4j. model. zipParameters; import net.lingala.zip 4j. util. zip4jConstants; import net.lingala.zip4j.zip. zipEngine; public class Test extends ZipEngine {priva Te ZipModel zipModel; public Test (ZipModel zipModel) throws ZipException {super (zipModel); this.zip Model = zipModel ;} /*** @ param args */public static void main (String [] args) {TestSendAttatch att = null; try {att = new TestSendAttatch (new ZipModel (); att. doZipEnc (); System. out. println ("successful");} catch (Exception e) {System. out. println ("failed"); e. printStackTrace () ;}} public void doZipEnc () {try {// Write the compressed stream to the memory ByteArrayOutputStream saos = new ByteArrayOutputStream (); // test FileOutputStream f = new FileOutputStream ("d: \ test1.zip", true) locally ); for (int I = 0; I <1; I ++) {String passwd = "111"; ZipParameters parameters = new ZipParameters (); parameters. setCompressionMethod (Zip4jConstants. COMP_DEFLATE); // The compression method parameters. setCompressionLevel (Zip4jConstants. DEFLATE_LEVEL_NORMAL); // compression level parameters. setS OurceExternalStream (true); parameters. setFileNameInZip ("aaaa _" + I + ". xls"); // if (! "". Equals (passwd) {parameters. setEncryptFiles (true); parameters. setEncryptionMethod (Zip4jConstants. ENC_METHOD_STANDARD); // The encryption method parameters. setPassword (passwd. toCharArray ();} addNewStreamToZipTmp (saos, parameters);} saos. writeTo (f); f. close (); saos. close (); // file size System. out. println ("size:" + (saos. toByteArray (). length/1024);} catch (Exception e) {e. printStackTrace () ;}} public void addNewS TreamToZipTmp (ByteArrayOutputStream baos, ZipParameters parameters) throws ZipException {ZipOutputStream outputStream = null; try {outputStream = new ZipOutputStream (baos, this.zip Model); if (zipModel. getEndCentralDirRecord () = null) {throw new ZipException ("invalid end of central directory record");} checkParameters (parameters); outputStream. putNextEntry (null, parameters); if (! Parameters. getFileNameInZip (). endsWith ("/")&&! Parameters. getFileNameInZip (). endsWith ("\") {for (int I = 0; I <10; I ++) {outputStream. write ("ccccccccccccccccccc ". getBytes (); outputStream. flush () ;}} outputStream. closeEntry (); outputStream. finish ();} catch (ZipException e) {throw e;} catch (Exception e) {throw new ZipException (e);} finally {if (outputStream! = Null) {try {outputStream. close ();} catch (IOException e) {}}} private void checkParameters (ZipParameters parameters) throws ZipException {if (parameters = null) {throw new ZipException ("cannot validate zip parameters");} if (parameters. getCompressionMethod ()! = Zip4jConstants. COMP_STORE) & parameters. getCompressionMethod ()! = Zip4jConstants. COMP_DEFLATE) {throw new ZipException ("unsupported compression type");} if (parameters. getCompressionMethod () = Zip4jConstants. COMP_DEFLATE) {if (parameters. getCompressionLevel () <0 & parameters. getCompressionLevel ()> 9) {throw new ZipException ("invalid compression level. compression level dor deflate shocould be in the range of 0-9 ") ;}} if (parameters. isEncryptFiles () {I F (parameters. getEncryptionMethod ()! = Zip4jConstants. ENC_METHOD_STANDARD & parameters. getEncryptionMethod ()! = Zip4jConstants. ENC_METHOD_AES) {throw new ZipException ("unsupported encryption method");} if (parameters. getPassword () = null | parameters. getPassword (). length <= 0) {throw new ZipException ("input password is empty or null") ;}} else {parameters. setAesKeyStrength (-1); parameters. setEncryptionMethod (-1 );}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.