Java Zip uncompressed __java

Source: Internet
Author: User

Recent Android projects require a built-in data file, zip compression to reduce the volume of packets, and the need to unzip the zip file at the client. summed up the information on the Internet predecessors, wrote a decompression zip tool class, in order to reuse.


Ziputils.java

Package com.example.zip;
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 */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 files generated by ZipFile * @throws Ioexc Eption throws/public static void Zipfiles (Collection<file> resfilelist, File ZipFile) throws when an error occurs in the compression process (IOEXCEP
                tion {zipoutputstream zipout = new Zipoutputstream (New Bufferedoutputstream (
        ZipFile), buff_size);
        for (File resfile:resfilelist) {zipfile (resfile, ZipOut, "");
    } zipout.close (); /** * Batch compressed files (clips) * * @param resfilelist List of files (folders) to compress * @param zipfile generated pressureShrink file * @param Comment Compressed file comments * @throws IOException throw/public static void Zipfiles when the compression process fails (Collection <File> resfilelist, File ZipFile, String comment) throws IOException {Zipoutputstream =
        New Zipoutputstream (New Bufferedoutputstream (New FileOutputStream (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 when
        Thrown/public static void Upzipfile (File zipfile, String folderpath) throws zipexception when the decompression process fails, 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 file * @param folderpath destination folder * @param name COntains incoming file matching name * @throws zipexception Compressed format error thrown * @throws IOException IO error throw/public static Arrayl Ist<file> Upzipselectedfile (File zipfile, String folderpath, String namecontains) throws Zipexception, I

        oexception {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 "Enter 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 Throw * @throws IOException When an error occurs in the decompression process * * public static arraylist<string> geTentriesnames (File zipfile) throws Zipexception, IOException {arraylist<string> entrynames = new ArrayList& Lt
        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 to obtain its properties * @param zipfile Compressed file * @return Returns a list of compressed files * @throws zipexcept Ion compressed file with incorrect format throw * @throws IOException IO operation is incorrect throw/public static enumeration<?> getentriesenumeration (
        File ZipFile) throws Zipexception, IOException {zipfile 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 Unsupportedencodin Gexception */public static String getEntrycomment (ZipEntry entry) throws Unsupportedencodingexception {return new String (Entry.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 Unsupportedencodin Gexception */public static String Getentryname (ZipEntry entry) throws Unsupportedencodingexception {RET
    Urn New String (Entry.getname (). GetBytes ("GB2312"), "8859_1"); 
     /** * Compressed File * * @param resfile files (folders) to be compressed * @param zipout Compressed file * @param rootpath Compressed Files path * @throws FileNotFoundException can not find the file thrown * @throws IOException when the compression process error, throw/private static void Zipfi
        Le (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 ();
 }
    }
}



Test class Test.java

Package com.example.zip;
Import Java.io.File;
Import java.io.IOException;
Import Java.util.Arrays;
Import java.util.List;

Import java.util.zip.ZipException; public class Test {/** * @param args */public static void main (string[] args) {String Sourcedirpath = "d:/fbb
		/test/";
		String Destdirpath = "d:/fbb/ziptest/";

		String fileName = "Test.zip";
		
		Testunzip (Sourcedirpath, FileName, Destdirpath);
	Testzip (Sourcedirpath,destdirpath); /** * Test Generation ZIP Package * @param sourcedirpath * @param destdirpath * * private static void Testzip (String sourcedirpa
		
		Th, String destdirpath) {file dir = new file (Sourcedirpath);
		String zipfilename = "Test.zip";
		
		File ZipFile = new file (destdirpath+zipfilename);
		
		List<file> resfilelist = null;
			if (dir.exists () && dir.isdirectory ()) {file[] files = dir.listfiles ();
		Resfilelist = arrays.aslist (files);
			
			try {ziputils.zipfiles (resfilelist, ZipFile); System.out.println ("ZIP Success ");
		catch (IOException e) {e.printstacktrace (); }/** * Test extract Zip Package * @param sourcedirpath * @param fileName * @param destdirpath/private static void Te Stunzip (String Sourcedirpath, String fileName, String destdirpath) {File ZipFile = new File (Sourcedirpath + filen

		AME);

			try {ziputils.upzipfile (ZipFile, Destdirpath);
		System.out.println ("unzip success");
		catch (Zipexception e) {e.printstacktrace ();
		catch (IOException e) {e.printstacktrace ();
 }
	}

}







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.