Android-File compression and decompression

Source: Internet
Author: User

Android-File compression and decompression
I am working on a project. There is a function in the project file that requires a lot of pictures. If the pictures are packaged with the app, there will be 30 + M. Then we will consider downloading the compressed package, we took out the image, and the project was only 4 + M. Hahaha, haha, It's really horrible. This involves downloading the compressed package and decompressing the package. Java tool classes also contain zip processing. Copy the public class Util {public static final String ZIP_FILENAME = "/data/com. yydcdut. unzip/new_ios_7_icons_by_sunkotora-d68qbmo.zip "; // name of the file to be decompressed public static final String ZIP_DIR ="/data/com. yydcdut. unzip/"; // The public static final String UN_ZIP_DIR ="/data/com. yydcdut. unzip/zzip/"; // directory of the file to be decompressed // the above two variables seem to be reversed. Public static final int BUFFER = 1024; // cache size/*** zip compression function. * compress all files under the baseDir (Folder directory), including sub-directories * @ throws Exception */public static void zipFile (String baseDir, String fileName) throws Exception {List fileList = getSubFiles (new File (baseDir); ZipOutputStream zos = new ZipOutputStream (new FileOutputStream (fileName); ZipEntry ze = null; byte [] buf = new byte [BUFFER]; int readLen = 0; for (int I = 0; I <fileList. s Ize (); I ++) {File f = (File) fileList. get (I); ze = new ZipEntry (getAbsFileName (baseDir, f); ze. setSize (f. length (); ze. setTime (f. lastModified (); zos. putNextEntry (ze); InputStream is = new BufferedInputStream (new FileInputStream (f); while (readLen = is. read (buf, 0, BUFFER ))! =-1) {zos. write (buf, 0, readLen);} is. close ();} zos. close ();}/*** returns the relative path of another file name for the given root directory, which is used for the path in the zip file. * @ param baseDir java. lang. string root directory * @ param realFileName java. io. file actual File name * @ return relative File name */private static String getAbsFileName (String baseDir, File realFileName) {File real = realFileName; File base = new File (baseDir ); string ret = real. getName (); while (true) {real = real. getParentFile (); if (Real = null) break; if (real. equals (base) break; else ret = real. getName () + "/" + ret;} return ret;}/*** get the list of all objects in the specified directory, including subdirectories. * @ param baseDir File specifies the directory * @ return contains java. io. file List */private static List getSubFiles (File baseDir) {List ret = new ArrayList (); File [] tmp = baseDir. listFiles (); for (int I = 0; I <tmp. length; I ++) {if (tmp [I]. isFile () ret. add (tmp [I]); if (tmp [I]. isDirectory () ret. addA Ll (getSubFiles (tmp [I]);} return ret;}/*** extract function. * decompress the ZIP_FILENAME file to the ZIP_DIR directory. * @ throws Exception */public static void upZipFile () throws Exception {ZipFile zfile = new ZipFile (ZIP_FILENAME); Enumeration zList = zfile. entries (); ZipEntry ze = null; byte [] buf = new byte [1024]; while (zList. hasMoreElements () {ze = (ZipEntry) zList. nextElement (); if (ze. isDirectory () {File f = new File (ZIP_DIR + ze. getNam E (); f. mkdir (); continue;} OutputStream OS = new BufferedOutputStream (new FileOutputStream (getRealFileName (ZIP_DIR, ze. getName (); InputStream is = new BufferedInputStream (zfile. getInputStream (ze); int readLen = 0; while (readLen = is. read (buf, 0, 1024 ))! =-1) {OS. write (buf, 0, readLen);} is. close (); OS. close ();} zfile. close ();}/*** returns the actual file name corresponding to a relative path for a given root directory. * @ param baseDir specify the root directory * @ param absFileName relative path name, from name * @ return java in ZipEntry. io. file the actual File */public static File getRealFileName (String baseDir, String absFileName) {String [] dirs = absFileName. split ("/"); File ret = new File (baseDir); if (dirs. length> 1) {for (int I = 0; I <dirs. length-1; I ++) {ret = new File (ret, dirs [I]);} if (! Ret. exists () ret. mkdirs (); ret = new File (ret, dirs [dirs. length-1]); return ret;} copy the code analysis and extract the code first. Use ZipFile to instantiate the compressed file, and then process ZipFile. Enumerate the elements in ZipFile and convert the elements in ZipFile into files by using IO operations. If it is a directory, create a directory to continue the operation. Compress, use ZipOutputStream to convert the file into an IO output stream, and then write the BufferedInputStream input stream.

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.