Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-18-2007, 01:30 PM
Member
 
Join Date: Dec 2007
Posts: 2
satishbejgum is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-20-2007, 11:08 AM
Member
 
Join Date: Dec 2007
Location: Roggwil, Switzerland
Posts: 8
daprodigy is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to read a text file from a Java Archive File Java Tip Java Tips 0 02-08-2008 11:13 AM
Create a new directory in existing Zip archive satishbejgum Advanced Java 1 12-23-2007 08:05 AM


All times are GMT +3. The time now is 11:05 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org