Solved

adding a file to a zip file

Posted on 2007-11-29
3
204 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

744 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

15 Experts available now in Live!

Get 1:1 Help Now