Results 1 to 2 of 2
Thread: Zip Archive Problem
- 12-18-2007, 11:30 AM #1
Member
- Join Date
- Dec 2007
- Posts
- 2
- Rep Power
- 0
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
- 12-20-2007, 09:08 AM #2
Member
- Join Date
- Dec 2007
- Location
- Roggwil, Switzerland
- Posts
- 8
- Rep Power
- 0
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
-
How to read a text file from a Java Archive File
By Java Tip in forum Java TipReplies: 0Last Post: 02-08-2008, 09:13 AM -
Create a new directory in existing Zip archive
By satishbejgum in forum Advanced JavaReplies: 1Last Post: 12-23-2007, 06:05 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks