logo

New Response

« Return to the main article

You are replying to:

  1. hello i m nikhil. i m trying to zip the contents of directory and the file which are in the sub directory .i have used java.util.zip. Programmes run without any error but when i open the newly zip file it only list the contents of files in the directory but programmes doesnt zip the subdirectory

    thnks nikhil below there is a code import java.util.*; import java.io.*; import java.util.zip.*; class DirTest { public void filePath(String strFilename) { String s[]=new String[0]; ArrayList arr=new ArrayList(); try { File f=new File(strFilename); if(f.isDirectory()) { s=f.list(); } for(int i=0;i<s.length;i++) { File f1=new File(f.getPath()+"/"+ s[i]); if(f1.isDirectory()) { filePath(f1.getPath()); } else { arr.add(f1.getPath()); } } arrListing(arr); } catch(Exception e) { System.out.println(e.getMessage()); } } public void arrListing(ArrayList a1) { try { FileOutputStream fout=new FileOutputStream("C:/myDir/ot.zip"); ZipOutputStream zout=new ZipOutputStream(fout); FileInputStream fin=null; Iterator itr=a1.iterator(); while(itr.hasNext()) { String str=(String) itr.next(); System.out.println(str); zout.setMethod(ZipOutputStream.DEFLATED); zout.setLevel(9); zout.putNextEntry(new ZipEntry(str)); fin=new FileInputStream(str); int c; while((c=fin.read())!=-1) { zout.write(c); } zout.flush(); }

    fin.close(); zout.closeEntry(); zout.close(); } catch(Exception e) { System.out.println(e); } } public static void main(String args[]) throws IOException { DirTest objDir=new DirTest(); objDir.filePath("C:/myDir"); } }

Your Comments

Name:
E-mail:
(optional)
Website:
(optional)
Comment: