Solved

adding a file to a zip file

Posted on 2007-11-29
3
206 Views
Last Modified: 2010-03-30
Here is basically my what my code is doing

Creates a XML file
Creates a zip file (XML file is not included)
FTPs the zip

What I need to do now is add the XML to the zip file.  I tried to do:
out.putNextEntry(new ZipEntry(XMLName));      

This put the file name in the zip file but with no data.  I am attaching my zip code
//***********************************************************Start ZIP       

        

        byte[] buf = new byte[1024];

	

	try {

	    // Create the ZIP file

	    String target = path;

	    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(target));

	 	

	    // Compress the files

	    for (int i=0; i<remember.size(); i++) {

		FileInputStream in = new FileInputStream(remember.elementAt(i));

		

		// Add ZIP entry to output stream.

		//out.putNextEntry(new ZipEntry(remember.elementAt(i)));

	        //New Zip suff

                if (remember.elementAt(i).lastIndexOf('\\') != -1) {

                slash = remember.elementAt(i).lastIndexOf('\\');

                } else {

                slash = remember.elementAt(i).lastIndexOf('/');

                }

                name = remember.elementAt(i).substring(slash + 1);

                out.putNextEntry(new ZipEntry(name));	

		

 

                // Transfer bytes from the file to the ZIP file

		int len;

		while ((len = in.read(buf)) > 0) {

		    out.write(buf, 0, len);

		}

		

		// Complete the entry

		out.closeEntry();

		in.close();

	    }

	    

	    // Complete the ZIP file

	    out.close();

	} catch (IOException e) {

	}

         

	System.out.println("Zip File is Completed"); 

//*****************************************************End ZIP

Open in new window

0
Comment
Question by:Jaziar
  • 2
3 Comments
 
LVL 17

Accepted Solution

by:
contactkarthi earned 125 total points
ID: 20376061
see the example here

http://www.exampledepot.com/egs/java.util.zip/CreateZip.html

you need to read and write the xml file to out
0
 
LVL 9

Expert Comment

by:ysnky
ID: 20376063
0
 
LVL 17

Expert Comment

by:contactkarthi
ID: 20376075
some thing like
FileInputStream in = new FileInputStream(filenames[i]);

    

            // Add ZIP entry to output stream.

            out.putNextEntry(new ZipEntry(filenames[i]));

    

            // Transfer bytes from the file to the ZIP file

            int len;

            while ((len = in.read(buf)) > 0) {

                out.write(buf, 0, len);

            }

    

            // Complete the entry

            out.closeEntry();

            in.close();

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now