Java-compressing folders into zip files and java folders into zip files
Import java. io. bufferedInputStream; import java. io. file; import java. io. fileInputStream; import java. io. fileOutputStream; import java. io. IOException; import org.apache.tools.zip. zipEntry; import org.apache.tools.zip. zipOutputStream; /***** @ author hwt **/public class TestDir {/*** compresses the folder and its files into files * @ param args * @ throws IOException */public static void main (String [] args) throws IOException {
// Source folder File = new file ("D:/ziptest ");
// Target file ZipOutputStream zos = new ZipOutputStream (new FileOutputStream ("D:/test.zip"); if (file. isDirectory () {File [] files = file. listFiles (); for (int I = 0; I <files. length; I ++) {BufferedInputStream bis = new BufferedInputStream (new FileInputStream (files [I]); zos. putNextEntry (new ZipEntry (file. getName () + File. separator + files [I]. getName (); while (true) {byte [] B = new byte [100]; int len = bis. read (B); if (len =-1) break; zos. write (B, 0, len);} bis. close () ;}} zos. close ();}}