|
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
|