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 ;}