Solved

adding a file to a zip file

Posted on 2007-11-29
3
211 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

734 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