Java unzip the zip file to the specified folder

Source: Internet
Author: User

Earlier, I mentioned. How to package the file as a zip package, then how to unzip the zip file package into a normal file? It is easier to unpack the zip package into a normal package than to package the file as a zip. Because there is a multilevel file compression, there is no multi-level file decompression. In other words, when compressing, you have to plug all the files into the compressed package. Decompression only needs to decompress the first level, the compressed package inside the compressed file will not be ignored.
directly on the code:

    /** * Unzip the file * @param zippath to extract the target file * @param descdir specified extract directory * @return Decompression results: Success, failure */@SuppressW Arnings ("Rawtypes") Public boolean Decompresszip (String Zippath, String descdir) {File ZipFile = new file (Zippa        TH);        Boolean flag = false;        File Pathfile = new file (Descdir);        if (!pathfile.exists ()) {pathfile.mkdirs ();        } ZipFile zip = null; try {zip = new ZipFile (ZipFile, Charset.forname ("GBK"));//Prevent Chinese directory, garbled for (enumeration entries = ZIP.E Ntries ();                Entries.hasmoreelements ();) {ZipEntry entry = (zipentry) entries.nextelement ();                String zipentryname = Entry.getname ();                InputStream in = Zip.getinputstream (entry);                Specify the extracted folder + the name of the current zip file String Outpath = (descdir+zipentryname). Replace ("/", file.separator); Determines whether the path exists, does not exist, creates a file path, and files = new (outpath.substring (0, Outpath.lastindexof ((File.separator)));                if (!file.exists ()) {file.mkdirs (); }//Determine if the full path of the file is a folder, if it is already uploaded above, do not need to unzip if (new file (Outpath). Isdirectory ()) {Co                Ntinue;                }//Save the file path information (you can use the Md5.zip name to determine if it has been decompressed) System.err.println ("The path after the current zip decompression is:" + Outpath);                OutputStream out = new FileOutputStream (Outpath);                byte[] buf1 = new byte[2048];                int Len;                while ((Len=in.read (BUF1)) >0) {out.write (Buf1,0,len);                } in.close ();            Out.close ();            } flag = true;            Must be closed, otherwise this zip file has been occupied, to delete the deletion, renaming can not, mobile also not, the whole lot, the system also collapsed.        Zip.close ();        } catch (IOException e) {e.printstacktrace ();    } return flag; }

Find an example to achieve this:
It's just you!

Call:

                String deal_zip = "C:\\20180909.zip";                String agter_zip = "D:\\red_ant_file";//解压完塞到这里吧                boolean is_success = AllServiceIsHere.decompressZip(deal_zip, agter_zip);                if(is_success) {                    System.err.println("恭喜你,解压成功!");                }else {                    System.err.println("sorry, you failed!");                }

Come on, you!

Well, that's what I asked for. Go to the market!

Java unzip the zip file to the specified folder

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.