Android comes with zip for easy compression decompression

Source: Internet
Author: User

The development process used a ZIP compression package, wrote a tool class, the class can be implemented to compress the string directly into the ZIP format, eliminating the need to write to the file to compress the steps:

/** * * @author shx * Compression and Decompression tool * */public class Ziputil {/** * compression method * @param str to compress the string * @param path PATH * @throws Ioexce ption */public static void compress (String str,string path) throws IOException {if (null = = str | | str.length () <= 0) { return;} FileOutputStream FileOutputStream = new FileOutputStream (path); Gzipoutputstream gzip = new Gzipoutputstream (fileoutputstream); Gzip.write (Str.getbytes ("Utf-8")); Gzip.close (); Fileoutputstream.close ();} /** * Unzip * @param context * @param path * @return */public static String uncompress (Context context,string path) {try {Fi Le file = new file (path), if (!file.exists ()) {return context.getresources (). getString (r.string.filenotexits);} Bytearrayoutputstream out = new Bytearrayoutputstream ();//Create a new output stream fileinputstream FileInputStream = new FileInputStream (path); Gzipinputstream gzip = new Gzipinputstream (FileInputStream); byte[] buffer = new Byte[256];int n = 0;//reads uncompressed data into a byte array while ( (n = gzip.read (buffer)) >= 0) {out.write (buffer, 0, n);} ReturnOut.tostring ("Utf-8");} catch (Exception e) {e.printstacktrace ();} return null;}


Android comes with zip for easy compression decompression

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.