Java uses ZipOutputStream for packaging

Source: Internet
Author: User

Java uses ZipOutputStream for packaging

 

 

Package A single file:

 

[Java]

  1. Public static void main (String [] args) throws IOException {

  2. File file = new File (d: + File. separator + aaa. java );

  3. File zipFile = new File (d: + File. separator + hello.zip );

  4. InputStream input = new FileInputStream (file );

  5. ZipOutputStream zipOut = new ZipOutputStream (new FileOutputStream (

  6. ZipFile ));

  7. ZipOut. putNextEntry (new ZipEntry (file. getName ()));

  8. // Set comments

  9. ZipOut. setComment (hello );

  10. Int temp = 0;

  11. While (temp = input. read ())! =-1 ){

  12. ZipOut. write (temp );

  13. }

  14. Input. close ();

  15. ZipOut. close ();

  16. }


     

     

    Package Multiple files:

     

    [Java]

    1. Public static void main (String [] args) throws IOException {

    2. // The folder to be compressed

    3. File file = new File (d: + File. separator + temp );

    4. File zipFile = new File (d: + File. separator + zipFile.zip );

    5. InputStream input = null;

    6. ZipOutputStream zipOut = new ZipOutputStream (new FileOutputStream (

    7. ZipFile ));

    8. ZipOut. setComment (hello );

    9. If (file. isDirectory ()){

    10. File [] files = file. listFiles ();

    11. For (int I = 0; I <files. length; ++ I ){

    12. Input = new FileInputStream (files [I]);

    13. ZipOut. putNextEntry (new ZipEntry (file. getName ()

    14. + File. separator + files [I]. getName ()));

    15. Int temp = 0;

    16. While (temp = input. read ())! =-1 ){

    17. ZipOut. write (temp );

    18. }

    19. Input. close ();

    20. }

    21. }

    22. ZipOut. close ();

    23. }


      We naturally think that since it can be compressed and decompressed, we will use a ZipFile class before talking about decompression. Let's give this example first. Every compressed file in java can be represented by ZipFile.

       

       

      [Java]

      1. Import java. io. File;

      2. Import java. io. IOException;

      3. Import java.util.zip. ZipFile;

      4.  

      5. /**

      6. * ZipFile demo

      7. **/

      8. Public class ZipFileDemo {

      9. Public static void main (String [] args) throws IOException {

      10. File file = new File (d: + File. separator + hello.zip );

      11. ZipFile zipFile = new ZipFile (file );

      12. System. out. println (compressed file name: + zipFile. getName ());

      13. }

      14. }


        Decompress a single file:

         

         

        [Java]

        1. Public static void main (String [] args) throws IOException {

        2. File file = new File (d: + File. separator + hello.zip );

        3. File outFile = new File (d: + File. separator + unZipFile.txt );

        4. ZipFile zipFile = new ZipFile (file );

        5. ZipEntry entry = zipFile.getEntry(hello.txt); // hello.txt indicates the name of the file in the compressed package.

        6. InputStream input = zipFile. getInputStream (entry );

        7. OutputStream output = new FileOutputStream (outFile );

        8. Int temp = 0;

        9. While (temp = input. read ())! =-1 ){

        10. Output. write (temp );

        11. }

        12. Input. close ();

        13. Output. close ();

        14. }


          Decompress multiple files:

           

          ZipEntry cannot be used when we need to extract multiple files. To perform more complex operations on compressed files, we must use the ZipInputStream class.

           

          [Java]

          1. Public static void main (String [] args) throws IOException {

          2. File file = new File (d: + File. separator + zipFile.zip );

          3. File outFile = null;

          4. ZipFile zipFile = new ZipFile (file );

          5. ZipInputStream zipInput = new ZipInputStream (new FileInputStream (file ));

          6. ZipEntry entry = null;

          7. InputStream input = null;

          8. OutputStream output = null;

          9. While (entry = zipInput. getNextEntry ())! = Null ){

          10. System. out. println (extract + entry. getName () + file); // entry. getName () obtain the file and file path in the compressed package (aaaa/bb.txt)

          11. OutFile = new File (d: + File. separator + entry. getName ());

          12. If (! OutFile. getParentFile (). exists ()){

          13. OutFile. getParentFile (). mkdir ();

          14. }

          15. If (! OutFile. exists ()){

          16. OutFile. createNewFile ();

          17. }

          18. Input = zipFile. getInputStream (entry );

          19. Output = new FileOutputStream (outFile );

          20. Int temp = 0;

          21. While (temp = input. read ())! =-1 ){

          22. Output. write (temp );

          23. }

          24. Input. close ();

          25. Output. close ();

          26. }

          27. }

          28.  

             

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.