Java does not understand compression directly read the. zip compressed File This example, the following demo of the project source generation download address is as follows:
Source code Download address
Package com.sd.test.readzip;
Import Java.io.BufferedInputStream;
Import Java.io.BufferedReader;
Import Java.io.FileInputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;
Import Java.util.zip.ZipEntry;
Import Java.util.zip.ZipFile;
Import Java.util.zip.ZipInputStream; Note The default save to save the file as Utf-8,txt file is ANSI. To be changed to Urf-8, it would be like File:ziptestfile/fileinzip.txt public class Readzip {public static void
Main (string[] args) throws IOException {//TODO auto-generated method stub readzip rz=new readzip ();
Rz.readzipcontext ();
The public void Readzipcontext () throws ioexception{String zippath= "Zipfile/ziptestfile.zip";
ZipFile zf=new ZipFile (Zippath);
InputStream in=new Bufferedinputstream (New FileInputStream (Zippath));
Zipinputstream zin=new Zipinputstream (in);
The ZipEntry class is used to represent a ZIP file entry.
ZipEntry ze; while ((Ze=zin.getnextentry ())!=null) {if (Ze.isdirectory ()){//Empty folder does nothing}else{System.err.println ("File:" +ze.getname () + "\nsize:" +ze.
GetSize () + "bytes");
if (Ze.getsize () >0) {BufferedReader reader;
try {reader = new BufferedReader (New InputStreamReader (Zf.getinputstream (Ze), "utf-8"));
String Line=null;
while ((Line=reader.readline ())!=null) {System.out.println (line);
} reader.close (); catch (IOException e) {//TODO auto-generated catch block E.printstackt
Race ();
}
}
}
}
}
}
Output:
file:ziptestfile/fileinzip.txt
size:395bytes
1 zipе
2 jsonݣ{"age": "List": [ "One", "two"], "List1": [{"Age":], "name": "Xiao Ming", "Sex": "Boy", "strudent": true},{"Age": "Name": "Xiao", "" Sex ":" Girl "," Strudent ": true}]," map ": {" one ": {" age ":" Name ":" Xiao Ming "," Sex ":" Boy "," Strudent ": true}," two ": {" Age ":", "name": "Xiao", "Sex": "Girl", "Strudent": true}, "name": "Da June", "Sex": "Boy", "strudent": false, "student" : null}
3 sdfadh
File:ziptestfile/fileinzip1.txt
size:408bytes
1, text in Zip
2, JSON data: {"Age": 21, " List ": [" One "," two "]," List1 ": [{" Age ":" Name ":" Xiao Ming "," Sex ":" Boy "," strudent ": true},{" Age ":", "name": "Xiao "", "Sex": "Girl", "Strudent": "True}", "map": {"one": {"age": "Name": "Xiao Ming", "Sex": "Boy", "Strudent": true}, " Two ': {"age": "Name": "Xiao", "Sex": "Girl", "Strudent": "True}", "Name": "Da June", "Sex": "Boy", "strudent": false, " Student ': null}
3, Sdfadh