Zip the files in the e:/source folder and copy them to the f:/folder.
1 import java. io. *; 2 import java.util.zip. zipEntry; 3 import java.util.zip. zipOutputStream; 4 5 public class DirCopy {6 public static void main (String [] args) throws Exception {7 dirZipCopy ("E:/source", "F:/source.zip "); 8} 9/** 10 * folder compression Backup 11 * @ param fromDir folder 12 * @ param toDir directory 13 * @ throws Exception14 */15 public static void dirZipCopy (String fromDir, string toDir) throws Exception {16 // create ZIP output stream 17 ZipOutputStream zos = new ZipOutputStream (new FileOutputStream (toDir); 18 // recursive processing folder 19 zipCopy (new File (fromDir), zos, ""); 20 zos. close (); 21} 22/** 23 * compressed copy file 24 * @ param fromDir file to be compressed 25 * @ param zos ZIP output stream 26 * @ param path relative to ZIP file path 27 * @ throws Exception28 */29 private static void zipCopy (File fromDir, zipOutputStream zos, String path) throws Exception {30 if (fromDir. exists () {31 if (fr OmDir. isDirectory () {32 path + = fromDir. getName () + "/"; 33 zos. putNextEntry (new ZipEntry (path); 34 File [] files = fromDir. listFiles (); 35 if (files! = Null) {36 for (int I = 0; I <files. length; I ++) {37 zipCopy (files [I], zos, path); 38} 39} 40} else {41 // putNextEntry (): start writing new ZIP file entries and locate the stream to the beginning of the entry data. 42 zos. putNextEntry (new ZipEntry (path + fromDir. getName (); 43 InputStream is = new FileInputStream (fromDir); 44 int len = 0; 45 byte [] B = new byte [1024]; 46 while (len = is. read (B ))! =-1) {47 zos. write (B, 0, len); 48 zos. flush (); 49} 50 is. close (); 51} 52} 53} 54}
File Replication
1 public class FileCopy2 {2 public static void main (String [] args) {3 fileCopy ("F:/abc. rmvb "," F:/source/abc. rmvb "); 4} 5 public static void fileCopy (String srcFileSource, String destFileSource) {6 try {7 // read the original file content 8 FileInputStream in = new FileInputStream (srcFileSource ); 9 FileOutputStream out = new FileOutputStream (destFileSource); 10 int len = 0; 11 byte [] indata = new byte [1024*10]; 12 while (len = in. r Ead (indata ))! =-1) {13 out. write (indata); 14} 15 in. close (); 16 out. close (); 17} catch (Exception e) {18 e. printStackTrace (); 19} 20} 21}