Results 1 to 2 of 2
  1. #1
    satishbejgum is offline Member
    Join Date
    Dec 2007
    Posts
    2
    Rep Power
    0

    Default Zip Archive Problem

    Hello Every one

    I trying to add some files to existing zip archive. The code is
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.*;
    import java.lang.*;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipException;
    import java.util.zip.ZipFile;
    import java.util.zip.ZipOutputStream;

    public class copy {




    public static void main(String[] args)
    {
    File filenew;

    try
    {
    // These are the files to include in the ZIP file
    // String[] filestozip = new String[]{"image1.jpg", "image2.jpg"};

    // Create a buffer for reading the files
    byte[] buf = new byte[1024];

    // Create the ZIP file
    String zipfile = "C:/Documents and Settings/satishb/Desktop/Budget.zip";
    ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipfile));
    File dir = new File("C:/Documents and Settings/satishb/Desktop/Tests/test1/");

    String[] filestozip = dir.list();
    // Compress the files

    for (int i=0; i<filestozip.length; i++) {

    FileInputStream fis = new FileInputStream(dir+"\\"+filestozip[i]);

    // Add ZIP entry to output stream.
    zos.putNextEntry(new ZipEntry(filestozip[i]));

    // Transfer bytes from the file to the ZIP file
    int len;
    while ((len = fis.read(buf)) > 0) {
    zos.write(buf, 0, len);
    }

    // Complete the entry
    zos.closeEntry();
    fis.close();
    }

    // Complete the ZIP file
    zos.close();
    } catch (FileNotFoundException ex)
    {
    ex.printStackTrace();
    } catch (IOException ex)
    {
    ex.printStackTrace();
    }

    }

    }

    This code is adding new files but my old files are removed. Can any one help to keep old files as it is?

    Thanks for your time

  2. #2
    daprodigy is offline Member
    Join Date
    Dec 2007
    Location
    Roggwil, Switzerland
    Posts
    8
    Rep Power
    0

    Default

    hello

    try using another constructor for creating the FileOutputStream-instance to append new files, otherwise the jvm replaces the existing zip-file:

    instead of: ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipfile));

    ...

    try: ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipfile, true));

    ... and see if the files are added.

    greets

Similar Threads

  1. How to read a text file from a Java Archive File
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 10:13 AM
  2. Create a new directory in existing Zip archive
    By satishbejgum in forum Advanced Java
    Replies: 1
    Last Post: 12-23-2007, 07:05 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •