Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 1 of 1

Thread: Not able to put some files inside Zip file

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Lightbulb Not able to put some files inside Zip file

    Hi All,

    Can any one please let me know how to zip a directory which contains .xml and .txt files with sub directories?

    I tried below code but, its placing all the .xml, .txt files from the directory and sub directory and the sub-directory itself.

    for ex: zip1 is a directory {123.xml, 234.xml and zip234 as sub-directory}

    Now, i want to zip this files and sub-directories i.e. ZipTest.zip file should contain 123.xml, 234.xml and zip234, zip345 as sub-directory.

    And zip234 sub-directory can contain some more sub-directory.

    public static void main(String[] args) {
    String zipFileName = "C:\\compressed3.zip";
    ZipOutputStream zout;
    try {
    zout = new ZipOutputStream(new FileOutputStream(zipFileName));
    createZipFile("C:\\testZip", zout);
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
     
     
    private static void createZipFile(String string, ZipOutputStream zout) {
    FileInputStream fis = null;
    try {
    /*ZipFile zipFile = new ZipFile(string);
    ZipEntry entry = zipFile.getEntry(string);*/
    File die = new File(string);
    File[] files = die.listFiles();
     
    for (int i =0; i<files.length;i++){
     
    if(files[i].isDirectory())
    {
    //if the File object is a directory, call this
    //function again to add its content recursively
    //String filePath = f.getPath();
    String filePath = files[i].getPath();
    createZipFile(filePath, zout);
    //loop again
    continue;
    }
    fis = new FileInputStream(files[i]);
    zout.putNextEntry(new ZipEntry(files[i].getName()));
     
    byte[] buffer = new byte[1024];
    int bytesRead;
     
    //Each chunk of data read from the input stream
    //is written to the output stream
    while ((bytesRead = fis.read(buffer)) > 0) {
    zout.write(buffer, 0, bytesRead);
    }
     
    //Close zip entry and file streams
    zout.closeEntry();
    }
     
    } catch (IOException e) {
    e.printStackTrace();
    } finally{
    try {
    zout.close();
    fis.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
    Please Advice.

    Thanks,
    Chinnu
    Last edited by chinnu; March 24th, 2011 at 06:14 AM. Reason: Trying to give better description


Similar Threads

  1. Replies: 1
    Last Post: March 22nd, 2011, 07:59 PM
  2. [SOLVED] Pointing to file inside project
    By M3ss1ah in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: March 9th, 2011, 08:18 AM
  3. how to read file name inside .gz file....
    By kathir0301 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: December 6th, 2010, 10:06 AM
  4. making a .bat file that compiles your java files
    By sp11k3t3ht3rd in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 24th, 2010, 01:55 AM
  5. [SOLVED] Why can't I write to file inside a doGet() method?
    By FailMouse in forum Java Servlet
    Replies: 1
    Last Post: July 7th, 2010, 02:15 AM

Tags for this Thread