Environment
Operating system: Win7
Java:jdk7 Demand
Multiple jars need to be packaged for easy uploading. Steps
The Java APIs we need to use here are: Zipoutputstream, ZipEntry. compression of individual files
public static void Main (string[] args) {//c:\\users\\yutao\\desktop\\pageage String zipfilename = "C:
\\Users\\yutao\\Desktop\\pageage\\ziptest.zip ";
String entry = "C:\\users\\yutao\\desktop\\pageage\\ggjob-searchfull.jar";
Compress (zipfilename, entry);
/** * Compressed file * @param zipfilename * @param entry * @author Yutao * @date May 24, 2017 2:21:11 * * private static void compress (string zipfilename, string entry) {try {Zipoutputstream Z
OS = new Zipoutputstream (new Bufferedoutputstream (New FileOutputStream (Zipfilename)));
Set the compression level Zos.setlevel (deflater.best_compression);
File Entryfile = new file (entry);
Entryfile.listfiles ();
if (!entryfile.exists ()) {System.out.println ("The entry file" + Entryfile.getabsolutepath ());
Zos.close ();
Return } zipentry ze = New ZipEntry (Entryfile.getname ());
System.out.println (Ze.getname ());//Barcode name System.out.println (Ze.getcomment ());
System.out.println (Ze.getcompressedsize ());
System.out.println (Ze.getsize ());
System.out.println (Ze.getmethod ());
System.out.println (Ze.gettime ());
System.out.println (ZE.GETCRC ());
System.out.println (Ze.getextra ());
Zos.putnextentry (Ze);
Bufferedinputstream bis = new Bufferedinputstream (new FileInputStream (Entryfile));
byte[] buffer = new byte[1024];
int count =-1;
Bis.read ();
while ((count = bis.read (buffer))!=-1) {zos.write (buffer, 0, count);
} bis.close ();
Zos.closeentry ();
Zos.close ();
catch (IOException e) {e.printstacktrace (); }
}
Multi-File compression
Below we want to compress the above situation, the code is as follows:
public static void Main (string[] args) throws Exception {String entry = "c:\\users\\yutao\\desktop\\pageage\
\test "//Need to compress files directory file = new file (entry); Zipoutputstream zipoutput = new Zipoutputstream (New Bufferedoutputstream (FileOutputStream ()
+ ". zip"));
String base = File.getname ();
Compresszip (zipoutput, file, base);
Zipoutput.closeentry ();
Zipoutput.close (); /** * Because there may be a folder in the subfolder, the recursive */private static void Compresszip (Zipoutputstream zipoutput, File fi Le, String base) throws IOException {if (File.isdirectory ()) {file[] listfiles = File.listfiles (); Lists all files for (file fi:listfiles) {if (Fi.isdirectory ()) {Compresszip (Zipou
Tput, FI, base + "/" + fi.getname ());
}else{zip (zipoutput, FI, base); }}else{Zip (Zipoutput, file, base); }/** * Compact operation */public static void Zip (Zipoutputstream zipoutput, file file, String Base) Throws IOException, FileNotFoundException {zipentry zentry = new ZipEntry (base + File.separator + file.getname ())
;
Zipoutput.putnextentry (Zentry);
Bufferedinputstream bis = new Bufferedinputstream (new FileInputStream (file));
byte[] buffer = new byte[1024];
int read = 0;
while (read =bis.read (buffer))!=-1) {zipoutput.write (buffer, 0, read);
} bis.close (); }
Key points:
①zipentry is actually the file information that is about to compress the file.
Suppose we are about to compress the Ggindex.jar file, the path is the root path.
So we're going to set it up in the ZipEntry object.
ZipEntry zentry = new ZipEntry (base + File.separator + file.getname ());
Zipoutput.putnextentry (zentry);
According to the above hypothesis, we will write
zipentry zentry = new ZipEntry ("Ggindex.jar");
Zipoutput.putnextentry (zentry);//Add it to the compression pack for compression.
Also note here that the ZipEntry parameter base + file.separator + File.getname () is designed to produce the following effect. Otherwise, all files will be in the root path.
Decompression
The key of decompression is to get zipentry by Zipinputstream object's Getnextentry () method. This is the compressed file (entry) in the compressed package.
public static void main (string[] args) throws Exception {String entryfile = "C:\\us
Ers\\yutao\\desktop\\pageage\\test.zip ";
File File = new file (entryfile);
Zipinputstream ZiS = new Zipinputstream (new Bufferedinputstream) (new FileInputStream (file));
File fout = null;
String parent = File.getparent ();
ZipEntry entry; while ((entry = Zis.getnextentry ())!=null &&!entry.isdirectory ()) {fout = new File (parent, Entry.getna
Me ());
if (!fout.exists ()) {(New File (Fout.getparent ())). Mkdirs ();
Bufferedoutputstream BOS = new Bufferedoutputstream (new FileOutputStream (Fout));
int b =-1;
byte[] buffer = new byte[1024];
while ((b = zis.read (buffer))!=-1) {bos.write (buffer, 0, B);
} bos.close ();
} zis.close (); }
Summary
Know the Java Zip compression principle, in fact, is very simple.
① compression must be used to Zipoutputstream and zipentry, decompression, must be used to Zipinputstream and ZipEntry.
② compression, including both files and directories, basically use recursion, the path must remember to set up well.
③ because zip compresses each file individually without using redundant information between files (that is, solid compression), the ZIP compression rate is less than the tar package.
Reference Address:
http://www.imooc.com/wenda/detail/254317
Http://www.cnblogs.com/lbangel/p/3459092.html
Http://www.yiibai.com/java_io/java_io_zip_file.html