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