Java operation ZIP file

Source: Internet
Author: User
Tags create zip

Article transferred from: http://www.abcjava.com/html/JAVA/JAVAjichu/2009/1018/1080.html

Http://www.xcoder.cn

When multiple files are transmitted over the network, it will take a lot of time and consume more computer system resources. If we package multiple files and transfer them again, it will save a lot of computer resources. Here, I will show you how to read, decompress, and create ZIP files. Of course, here is a brief introduction. You need to implement the ZIP file in many details. For Java operations on zips, the main function is to use classes in the java.util.zip package.

1. Read ZIP files

The zipfile class is used to read entries from a zip file. Therefore, you must first create a zipfile object, and then use the entries () method of the zipfile object to obtain all entries in the ZIP file. The Code is as follows:

 
 
  1. Public static void readzipfile (File file ){
  2. Try {
  3. Zipfile = new zipfile (File); // create a zipfile object
  4. System. Out. println (zipfile. getname (); // print the ZIP file path.
  5. System. Out. println ("Zip entries:" + zipfile. Size (); // print the number of zip entries
  6. Enumeration <? Extends zipentry> en = zipfile. Entries ();
  7. Zipentry entry;
  8. While (EN. hasmoreelements ()){
  9. Entry = en. nextelement ();
  10. System. Out. println (entry. getname ());
  11. }
  12. Zipfile. Close ();
  13. } Catch (zipexception e ){
  14. System. Out. println (file. getname () + "compressed file error ");
  15. } Catch (ioexception e ){
  16. System. Out. println (file. getname () + "An error occurred while reading the file ");
  17. }
  18. }

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.