ZIP file compression decompression for Android development

Source: Internet
Author: User
Tags zip
The code is as follows Copy Code

-----------------Dirtraversal.java
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> listlinkedfiles (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 arraylist<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].getabsolutepath ());
} else {
if (Files[i].getname (). toLowerCase (). EndsWith ("Zip"))
Filelist.add (Files[i]);
}
}
return filelist;
}
}

-----------------Ziputils.java
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 of the Zip tool
*
* @author Once
*/
public class Ziputils {
private static final int buff_size = 1024 * 1024; 1M Byte

/**
* Batch compressed file (clip)
*
* @param resfilelist List of files (folders) to compress
* @param compressed file generated by ZipFile
* @throws IOException When the compression process is wrong
*/
public static void Zipfiles (Collection<file> resfilelist, File zipfile) throws IOException {
Zipoutputstream zipout = new Zipoutputstream (new Bufferedoutputstream, New FileOutputStream (
ZipFile), buff_size);
for (File resfile:resfilelist) {
ZipFile (Resfile, ZipOut, "");
}
Zipout.close ();
}

/**
* Batch compressed file (clip)
*
* @param resfilelist List of files (folders) to compress
* @param compressed file generated by ZipFile
* @param Comment Compressed file comments
* @throws IOException When the compression process is wrong
*/
public static void Zipfiles (Collection<file> resfilelist, File ZipFile, String comment)
Throws IOException {
Zipoutputstream zipout = new Zipoutputstream (new Bufferedoutputstream, New FileOutputStream (
ZipFile), buff_size);
for (File resfile:resfilelist) {
ZipFile (Resfile, ZipOut, "");
}
Zipout.setcomment (comment);
Zipout.close ();
}

/**
* Unzip a file
*
* @param zipfile Compressed files
* @param the target directory of FolderPath decompression
* @throws IOException When the decompression process fails
*/
public static void Upzipfile (File zipfile, String folderpath) throws Zipexception, IOException {
File Desdir = new 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 files
* @param folderpath target folder
* @param namecontains incoming file match name
* @throws zipexception compressed format is incorrect
* Thrown when @throws IOException IO error
*/
public static arraylist<file> Upzipselectedfile (File zipfile, String FolderPath,
String namecontains) throws Zipexception, IOException {
arraylist<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;
}

/**
* Obtain compressed file list
*
* @param zipfile Compressed file
* @return Compressed file name
* @throws zipexception Compressed file format error Throws the
* @throws ioexception
/
public static arraylist<string> getentriesnames (File zipfile) when the decompression process fails Throws Zipexception, IOException {
arraylist<string> entrynames = new arraylist<string> ();
Enumeration<?> entries = getentriesenumeration (ZipFile);
while (entries.hasmoreelements ()) {
ZipEntry entry = ((ZipEntry) entries.nextelement ());
Entrynames.add (Getentryname (Entry). GetBytes ("GB2312"), "8859_1");
}
Return entrynames;
}

/**
* Get the compressed file inside the compressed files object to obtain its properties
*
* @param zipfile Compressed files
* @return return a list of compressed files
* @throws zipexception Compressed file is incorrectly formatted
* @throws IOException IO operation is incorrect
*/
public static enumeration<?> getentriesenumeration (File zipfile) throws Zipexception,
IOException {
ZipFile ZF = new ZipFile (ZipFile);
return Zf.entries ();

}

/**
* Get a comment on the compressed file object
*
* @param entry Compressed File object
* @return The annotation of the compressed file object
* @throws unsupportedencodingexception
*/
public static String Getentrycomment (ZipEntry entry) throws Unsupportedencodingexception {
return new String (Entry.getcomment (). GetBytes ("GB2312"), "8859_1");
}

/**
* Get 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");
}

/**
* Compress files
*
* @param resfile files (clips) that need to be compressed
* @param zipout Compressed destination file
* @param rootpath Compressed file path
* @throws FileNotFoundException can not find the file when it is thrown
* @throws IOException When the compression process is wrong
*/
private static void ZipFile (File resfile, Zipoutputstream zipout, String RootPath)
Throws FileNotFoundException, IOException {
RootPath = RootPath + (Rootpath.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 ();
}
}
}

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.