Use Java zip to compress a single file

Source: Internet
Author: User

Import java.util.zip. zipoutputstream;
Import java. Io. ioexception;
Import java.util.zip. zipentry;
Import java. Io. fileoutputstream;
Import java. Io. fileinputstream;

Class csvzip {

Public static void zip (string file, string zipfile ){
Try {
// Create a file input stream object
Fileinputstream in = new fileinputstream (File); // 0
// Create a file output stream object
Fileoutputstream out = new fileoutputstream (zipfile); // 1
// Create a zip data output stream object
Zipoutputstream zipout = new zipoutputstream (out );
// Create an entry pointing to the compressed original file
Zipentry entry = new zipentry (File); // 0
Zipout. putnextentry (entry );
// Output data to the compressed file
Int nNumber;
Byte [] buffer = new byte [512];
While (nNumber = in. Read (buffer ))! =-1)
Zipout. Write (buffer, 0, nNumber );
// Close the created Stream Object
Zipout. Close ();

Out. Close ();
In. Close ();

} Catch (ioexception e ){
System. Out. println (E );
}

}
}

 

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.