Solved

adding a file to a zip file

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
eclipse buid path vs tomcat lib path 10 38
Java basic valueOf question 1 33
Java array 21 82
Java List 4 36
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

749 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