Imitation 360 compression tool written in Java-original

Source: Internet
Author: User

Step 1: Create a compression format and compress byte


Package util; import java. io. bufferedInputStream; import java. io. bufferedOutputStream; import java. io. file; import java. io. fileInputStream; import java. io. fileOutputStream; import java. io. IOException; import java. io. inputStream; import java. io. outputStream; import java. util. collection; import java. util. enumeration; import java.util.zip. zipFile; import org.apache.tools.zip. zipEntry; import org.apache.tools.zip. Z IpOutputStream;/***** @ author huyongjian Oracle (Compus Solution Group) * @ Date 2013-8-2 * @ version 2.0 * @ since JDK1.6 recommended) Copy Right Information compus solution group ide: eclipse class: */public class Compression {private static final int BUFF_SIZE = 1024*1024; // 1 M Byte/*** batch Compression folder) * @ param resFileList: the folder to be compressed.) list * @ param zipFile: The generated compressed file * @ throws IOException. When an error occurs during the compression process, it throws */public static voi. D zipFiles (Collection <File> resFileList, File zipFile) throws IOException {ZipOutputStream zipout = new ZipOutputStream (new BufferedOutputStream (new FileOutputStream (zipFile), BUFF_SIZE); for (File resFile: resFileList) {zipFile (resFile, zipout, "");} zipout. close ();}/*** batch compression folder) * @ param resFileList the folder to be compressed) list * @ param zipFile: the compressed file generated * @ param comment of the compressed file * @ throws IOException */Public static void zipFiles (Collection <File> resFileList, File zipFile, String comment) throws IOException {ZipOutputStream zipout = new ZipOutputStream (new BufferedOutputStream (new FileOutputStream (zipFile ), BUFF_SIZE); for (File resFile: resFileList) {zipFile (resFile, zipout, "");} zipout. setComment (comment); zipout. close ();}/*** decompress a file * @ param zipFile compressed file * @ param folderPath Decompress the target directory * @ throws IOException when the compression process fails */public static void upZipFile (File zipFile, String folderPath) throws IOException {ZipFile zf = new ZipFile (zipFile ); for (Enumeration entries = zf. entries (); entries. hasMoreElements ();) {ZipEntry entry = (ZipEntry) entries. nextElement (); InputStream in = zf. getInputStream (entry); OutputStream out = new FileOutputStream (folderPath + File. separator + Entry. getName (); byte buffer [] = new byte [BUFF_SIZE]; int realLength; while (realLength = in. read (buffer)> 0) {out. write (buffer, 0, realLength);} in. close (); out. close () ;}} private static void zipFile (File resFile, ZipOutputStream zipout, String rootpath) throws IOException {rootpath = rootpath + (rootpath. trim (). length () = 0? "": File. separator) + resFile. getName (); if (resFile. isDirectory () {File [] fileList = resFile. listFiles (); for (File file: fileList) {zipFile (file, zipout, rootpath) ;}} else {byte buffer [] = new byte [BUFF_SIZE]; bufferedInputStream in = new BufferedInputStream (new FileInputStream (resFile), BUFF_SIZE); zipout. putNextEntry (new ZipEntry (rootpath); int realLength; while (realLength = in. read (Buffer ))! =-1) {zipout. write (buffer, 0, realLength);} in. close (); zipout. flush (); zipout. closeEntry ();}}}

Step 2: simple test


Package test; import java. io. file; import java. io. IOException; import java. util. arrayList; import java. util. collection; import util. compression;/***** @ author huyongjian Oracle (Compus Solution Group) * @ Date 2013-7-25 * @ version 2.0 * @ since JDK1.6 recommended) Copy Right Information compus solution group ide: eclipse class: */public class CompressionTest {public static void main (String [] args) throws IOException {Collection <File> resFileList = new ArrayList <File> (); resFileList. add (new File ("C :\\ this is a test File .doc"); File zipFile = new File ("C: \ txxxt.zip"); Compression.zip Files (resFileList, zipFile );}}


The reader creates a file on your drive C ------ this is a test file .doc------run compressiontest.java file. This txxxt.zip file is displayed on your drive C.


650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/11191459A-0.jpg "title =" compressed .jpg "/>

But opening our file is garbled.


650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1119144154-1.jpg "title =" garbled .jpg "/>

This problem is well solved:

Use the following two packages to replace the preceding two packages.

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1119143949-2.jpg "title =" qq 30213175945.jpg "/>

I will upload these two packages below!

Java -160243674 welcome to join us!

This article from the "promise always attachment Xiao Chai," blog, please be sure to keep this source http://1936625305.blog.51cto.com/6410597/1282142

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.