I encountered a File compression problem yesterday. I found a lot of related materials and examples on the Internet. I finally found a good example and made some small changes.
In this example, all files in a folder can be compressed as follows:
Import Java. io. file; <br/> Import Java. io. fileinputstream; <br/> Import Java. io. fileoutputstream; </P> <p> Import org.apache.tools.zip. zipoutputstream; </P> <p> public class test {</P> <p>/** <br/> * @ Param inputfilename enter a folder. <br/> * @ Param zipfilename: Output a compressed folder, name of the packaged file <br/> * @ throws exception <br/> */<br/> Public void zip (string inputfilename, string zipfilename) throws exception {<br/> system. Out. println (zipfilename); <br/> zip (zipfilename, new file (inputfilename); <br/>}</P> <p> private void zip (string zipfilename, file inputfile) throws exception {<br/> zipoutputstream out = new zipoutputstream (New fileoutputstream (<br/> zipfilename); <br/> zip (Out, inputfile, ""); <br/> system. out. println ("Zip done"); <br/> out. close (); <br/>}</P> <p> private void zip (zipoutputstream out, file F, string B ASE) throws exception {<br/> If (F. isdirectory () {// determine whether it is a directory <br/> file [] FL = f. listfiles (); <br/> out. putnextentry (New org.apache.tools.zip. zipentry (base + "/"); <br/> base = base. length () = 0? "": Base + "/"; <br/> for (INT I = 0; I <FL. length; I ++) {<br/> zip (Out, FL [I], base + FL [I]. getname (); <br/>}< br/>}else {// compress all files in the directory <br/> out. putnextentry (New org.apache.tools.zip. zipentry (base); <br/> fileinputstream in = new fileinputstream (f); <br/> int B; <br/> system. out. println (base); <br/> while (B = in. read ())! =-1) {<br/> out. write (B); <br/>}< br/> in. close (); <br/>}</P> <p> Public static void main (string [] temp) {<br/> string inputfilename = "D: // temp // test"; // The folder to be compressed <br/> string zipfilename = "D: // temp // test.zip "; // compressed zip file </P> <p> test book = new test (); <br/> try {<br/> book.zip (inputfilename, zipfilename); <br/>}catch (exception ex) {<br/> ex. printstacktrace (); <br/>}</P> <p >}< br/>
Org.apache.tools.zip. zipoutputstream
The package is in ant. jar.
Enter the folder to be compressed and the compressed file name!
In addition, this program can also be used to compress a certain number of files. The program is slightly modified to pass in several file paths to be compressed. As follows:
Private void zip (zipoutputstream out, list listfile, string base) throws exception {</P> <p> out. putnextentry (New org.apache.tools.zip. zipentry (base + "/"); <br/> base = base. length () = 0? "": Base + "/"; <br/> file filename = NULL; <br/> for (INT I = 0; I <listfile. size (); I ++) {<br/> filename = new file (string) listfile. get (I); </P> <p> out. putnextentry (New org.apache.tools.zip. zipentry (base + filename. getname (); <br/> fileinputstream in = new fileinputstream (filename); <br/> int B; <br/> system. out. println (base); <br/> while (B = in. read ())! =-1) {<br/> out. write (B); <br/>}< br/> in. close (); <br/>}</P> <p>}
In addition, this day I found that org.apache.tools.zip.zipoutputstreampackage can be converted into java.util.zip. zipoutputstream, so it is better to use the latter and there is no need to import other jar packages.
Pipeline. zipoutputstream is normal, so pay attention to this !!!