The method of compressing and decompressing zip files with Android _android

Source: Internet
Author: User

This paper illustrates the method of compressing and decompressing the zip file with Android. Share to everyone for your reference. Specifically as follows:

Dirtraversal.java is as follows:

Package com.once;
Import Java.io.File;
Import java.util.ArrayList;
Import java.util.LinkedList; /** * Folder Traversal * @author Once * * */public class Dirtraversal {//no recursion public static linkedlist<file> Li
    Stlinkedfiles (String strpath) {linkedlist<file> list = new linkedlist<file> ();
    File dir = new file (strpath);
    File file[] = Dir.listfiles ();
      for (int i = 0; i < file.length i++) {if (File[i].isdirectory ()) List.add (file[i));
    Else System.out.println (File[i].getabsolutepath ());
    } File tmp;
      while (!list.isempty ()) {tmp = (File) list.removefirst ();
        if (Tmp.isdirectory ()) {file = Tmp.listfiles ();
        if (file = = null) continue;
          for (int i = 0; i < file.length i++) {if (File[i].isdirectory ()) List.add (file[i));
        Else System.out.println (File[i].getabsolutepath ()); } else {System.out.println (Tmp.getabsolutepath ());
  } return list;
  //recursion public static arraylist<file> listfiles (String strpath) {return refreshfilelist (strpath); public static arraylist<file> refreshfilelist (String strpath) {arraylist<file> filelist = new Arrayli
    St<file> ();
    File dir = new file (strpath);
    file[] files = dir.listfiles ();
    if (files = null) return null; for (int i = 0; i < files.length i++) {if (Files[i].isdirectory ()) {refreshfilelist (Files[i].getabsolu
      Tepath ());
      } else {if (Files[i].getname (). toLowerCase (). EndsWith ("Zip")) Filelist.add (files[i));
  } return filelist;

 }
}

The

Ziputils.java is as follows:

Package com.once;
Import java.io.*;
Import java.util.ArrayList;
Import java.util.Collection;
Import java.util.Enumeration;
Import Java.util.zip.ZipEntry;
Import java.util.zip.ZipException;
Import Java.util.zip.ZipFile;
Import Java.util.zip.ZipOutputStream; /** * Java utils Implementation Zip Tool * * @author once/public class Ziputils {private static final int buff_size = 1024 * 10 24; 1M Byte/** * Batch compressed file (clip) * * @param resfilelist List of files (folders) to compress * @param compressed files generated by ZipFile * @throws ioexcept
    Ion throws/public static void Zipfiles (Collection<file> resfilelist, File zipfile) throws IOException { Zipoutputstream zipout = new Zipoutputstream (new Bufferedoutputstream FileOutputStream (ZipFile), BUFF_SIZ
    E));
    for (File resfile:resfilelist) {zipfile (resfile, ZipOut, "");
  } zipout.close ();
   /** * Batch compressed files (clips) * * @param resfilelist List of files (folders) to compress * @param zipfile generated compressed file * @param Comment compressed file comments * @throws IoexceptiOn thrown/public static void Zipfiles (Collection<file> resfilelist, File ZipFile, String comment) when the compression process fails
        Throws IOException {Zipoutputstream zipout = new Zipoutputstream (New Bufferedoutputstream (
    ZipFile), buff_size);
    for (File resfile:resfilelist) {zipfile (resfile, ZipOut, "");
    } zipout.setcomment (comment);
  Zipout.close ();
   /** * Extract a file * * @param zipfile Compressed file * @param folderpath uncompressed target directory * @throws IOException thrown when an error occurred in the decompression process */public static void Upzipfile (File zipfile, String folderpath) throws Zipexception, IOException {File Desdir = n
    EW File (FolderPath);
    if (!desdir.exists ()) {desdir.mkdirs ();
    ZipFile ZF = new ZipFile (ZipFile); for (enumeration<?> entries = Zf.entries (); entries.hasmoreelements ();)
      {ZipEntry entry = (zipentry) entries.nextelement ());
      InputStream in = Zf.getinputstream (entry); String str = folderpath + File.separaTor + entry.getname ();
      str = new String (str.getbytes ("8859_1"), "GB2312");
      File Desfile = new file (str);
        if (!desfile.exists ()) {File Fileparentdir = Desfile.getparentfile ();
        if (!fileparentdir.exists ()) {fileparentdir.mkdirs ();
      } desfile.createnewfile ();
      } outputstream out = new FileOutputStream (desfile);
      byte buffer[] = new Byte[buff_size];
      int reallength;
      while ((Reallength = in.read (buffer)) > 0) {out.write (buffer, 0, reallength);
      } in.close ();
    Out.close (); /** * Extract file name containing incoming text * * @param zipfile Compressed file * @param folderpath destination folder * @param namecontains incoming file horse Match name * @throws zipexception the compressed format is incorrect when thrown * @throws IOException IO error throw/public static arraylist<file> Upzips Electedfile (File zipfile, String folderpath, String namecontains) throws Zipexception, IOException {ARRAYLIST&L T file> filelist = new Arraylist<filE> ();
    File Desdir = new file (FolderPath);
    if (!desdir.exists ()) {Desdir.mkdir ();
    ZipFile ZF = new ZipFile (ZipFile); for (enumeration<?> entries = Zf.entries (); entries.hasmoreelements ();)
      {ZipEntry entry = (zipentry) entries.nextelement ());
        if (Entry.getname (). Contains (Namecontains)) {InputStream in = Zf.getinputstream (entry);
        String str = folderpath + file.separator + entry.getname ();
        str = new String (str.getbytes ("8859_1"), "GB2312");
        Str.getbytes ("GB2312"), "8859_1" Output//Str.getbytes ("8859_1"), "GB2312" input file Desfile = new file (str);
          if (!desfile.exists ()) {File Fileparentdir = Desfile.getparentfile ();
          if (!fileparentdir.exists ()) {fileparentdir.mkdirs ();
        } desfile.createnewfile ();
        } outputstream out = new FileOutputStream (desfile);
        byte buffer[] = new Byte[buff_size];
 int reallength;       while ((Reallength = in.read (buffer)) > 0) {out.write (buffer, 0, reallength);
        } in.close ();
        Out.close ();
      Filelist.add (Desfile);
  } return filelist; /** * Get compressed file list * * @param zipfile Compressed file * @return Compressed file name * @throws zipexception Compressed file format error * Throws IOException throws/public static arraylist<string> Getentriesnames (File zipfile) throws when an error occurs in the decompression process
    tion, IOException {arraylist<string> entrynames = new arraylist<string> ();
    enumeration<?> entries = getentriesenumeration (ZipFile);
      while (Entries.hasmoreelements ()) {ZipEntry entry = ((ZipEntry) entries.nextelement ());
    Entrynames.add (New String (Getentryname (Entry). GetBytes ("GB2312"), "8859_1");
  return entrynames; /** * Get compressed file object to get its properties * * @param zipfile Compressed file * @return return a list of compressed files * @throws zipexception compressed file format incorrectly
throw * @throws IOException io operation is wrong to throw * *  public static enumeration<?> getentriesenumeration (File zipfile) throws Zipexception, IOException {ZipF
    Ile ZF = new ZipFile (ZipFile);
  return Zf.entries ();
   /** * Get a comment on a compressed file object * * @param entry Compressed File object * @return A comment for a compressed file object * @throws unsupportedencodingexception */public static String Getentrycomment (ZipEntry entry) throws Unsupportedencodingexception {return new string (ent
  Ry.getcomment (). GetBytes ("GB2312"), "8859_1");
   /** * Gets the name of the compressed file object * * @param entry Compressed File object * @return The name of the compressed file object * @throws unsupportedencodingexception */public static String Getentryname (ZipEntry entry) throws Unsupportedencodingexception {return new string (entry.
  GetName (). GetBytes ("GB2312"), "8859_1");  /** * Compressed File * * @param resfile files (clips) that need to be compressed * @param zipout Compressed destination file * @param rootpath Compressed file path * @throws FileNotFoundException Throw * @throws When the file is not found IOException when the compression process goes wrong, throw/private static void ZipFile (file resfile,Zipoutputstream zipout, String rootpath) throws FileNotFoundException, IOException {rootpath = RootPath + (root Path.trim (). Length () = 0?
    "": file.separator) + resfile.getname ();
    RootPath = new String (rootpath.getbytes ("8859_1"), "GB2312");
      if (Resfile.isdirectory ()) {file[] filelist = Resfile.listfiles ();
      for (file file:filelist) {zipfile (file, zipout, RootPath);
      } else {byte buffer[] = new Byte[buff_size];
      Bufferedinputstream in = new Bufferedinputstream (new FileInputStream (resfile), buff_size);
      Zipout.putnextentry (New ZipEntry (RootPath));
      int reallength;
      while ((Reallength = in.read (buffer))!=-1) {zipout.write (buffer, 0, reallength);
      } in.close ();
      Zipout.flush ();
    Zipout.closeentry ();

 }
  }
}

I hope this article will help you with your Android program.

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.