Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


Java Programming Code Examples

Java > Java Util Package Code Examples

Creating a ZIP file

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
Creating a ZIP file public class ZipFolder { ZipOutputStream zos; // Output stream for destination zip file. ZipFolder(File src, File des) { zipData(src,des); } public static void main(String args[]) { try { File src = new File(args[0]); File des = new File(args[1]); if(!args[1].toLowerCase().substring(args[1].length() - 4, args[1].length()).equalsIgnoreCase(".zip")) throw new ArrayIndexOutOfBoundsException(); new ZipFolder(src,des); } catch (ArrayIndexOutOfBoundsException aioobe) { System.out.println("The Usage of this command:\n ZipFolder \n Ex: ZipFolder c:\\backup c:\\feb2002.zip"); } } public void zipData(File source,File destination) { try { File zipfile = new File(destination.getPath()); zos = new ZipOutputStream( new FileOutputStream(zipfile)); File arrFile[]; arrFile = source.listFiles(); if (arrFile.length >0) { checkFile(source); //Calling recursively zos.close(); System.out.println("Winzip file is created successfully"); } else System.out.println("There is no file in the specified source"); }catch(Exception ze) { System.out.println("Exception occured in zipping file. Details :\n" + ze.getMessage()); } } public void writeToZip(File src) throws DataFormatException, ZipException, FileNotFoundException, IOException { // This function write s the data in to Zipoutput Stream. byte b[] = new byte[512]; FileInputStream is = new FileInputStream(src); ZipEntry ze = new ZipEntry(src.toString()); zos.putNextEntry(ze); int len = 0; while ((len =is.read(b))!= -1) zos.write(b,0,len); zos.closeEntry(); System.out.println("Zipping file " + src + " is done"); is = null; } public void checkFile(File fo) { /*This is a recursive function. If the File object is a file then it calls the function to write to zipoutput stream. If it is a directory it gets the list of file objects in the child directory and calls itself */ try { if(fo.isFile()) writeToZip(fo); else { File foarr[]; foarr = fo.listFiles(); for (int i=0; i checkFile(foarr[i]); } }catch(java.util.zip.DataFormatException dfe) { System.out.println("Input data is not in proper format. Details :\n" + dfe.getMessage()); }catch(java.util.zip.ZipException ze) { System.out.println("Exception occured in zipping file. Details :\n" + ze.getMessage()); }catch(java.io.FileNotFoundException fnfe) { System.out.println("File is missing. Details :\n" + fnfe.getMessage()); }catch(java.io.IOException ioe) { System.out.println("Input / Output problem. Details :\n" + ioe.getMessage()); } } }