Java does not need to decompress traversal and read the contents of the zip file

Source: Internet
Author: User

Code:

Package Com.wicresoft.jpo;
Import Java.io.BufferedInputStream;
Import Java.io.BufferedReader;
Import Java.io.FileInputStream;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;
Import Java.util.zip.ZipEntry;
Import Java.util.zip.ZipFile;
Import Java.util.zip.ZipInputStream; public class Readziputil {/** * @param args */public static void main (string[] args) {//TODO auto-generated Me
		Thod stub try {readzipfile ("D://test//test.zip");
		catch (Exception e) {//TODO auto-generated catch block E.printstacktrace ();
		The public static void Readzipfile (String file) throws Exception {ZipFile ZF = new ZipFile (file);
		InputStream in = new Bufferedinputstream (new FileInputStream (file));
		Zipinputstream Zin = new Zipinputstream (in);
		ZipEntry ze; while (ze = zin.getnextentry ())!= null) {if (Ze.isdirectory ()) {//System.out.print ("directory-" + Ze.getname
				() + ":"//+ ze.getsize () + "bytes");
			System.out.println (); } else {
				System.err.println ("file-" + ze.getname () + ":" + ze.getsize () + "bytes");
				Long size = Ze.getsize ();
					if (Size > 0) {bufferedreader br = new BufferedReader (New InputStreamReader (Zf.getinputstream (Ze));
					String Line;
					while (line = Br.readline ())!= null) {System.out.println (line);
				} br.close ();
			} System.out.println ();
	} zin.closeentry ();
 }
}

An error may occur when the above code traverses the file in the zip: Java java.util.zip.ZipException:invalid entry size. The reason for the mistake is not clear.

Workaround: Change the way the file is traversed in the zip.

ZipFile ZF = new ZipFile (apkpath);		
		
		enumeration<? Extends zipentry> zes = Zf.entries ();
		while (Zes.hasmoreelements ()) {
			ZipEntry ze1 = Zes.nextelement ();
			Perform other Actions
		}


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.