Android built-in zip for easy compression and decompression

Source: Internet
Author: User

Android built-in zip for easy compression and decompression

In the development process, a zip package is used and a tool class is written. This class can be used to directly compress strings into a zip format, eliminating the need to write files and then compress them:

/***** @ Author shx * compression and decompression tool **/public class ZipUtil {/***** Compression Method * @ param str the string to be compressed * @ param path * @ throws IOException */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 ("ut F-8 "); gzip. close (); fileOutputStream. close ();}/*** decompress * @ param context * @ param path * @ return */public static String unCompress (Context context, String path) {try {File file = new File (path); if (! File. exists () {return context. getResources (). getString (R. string. fileNotExits);} bytes 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; // read uncompressed data into the byte array while (n = gzip. read (buffer)> = 0) {out. write (buffer, 0, n);} return out. toString ("UTF-8");} catch (Exception e) {e. printStackTrace ();} return null ;}


Related Article

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.