Solved

adding a file to a zip file

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SHA2 certs for IIS AND Java? 2 136
Java Eclipse Loop 3 47
junit example issue 2 54
What does this method description mean? 1 11
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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

732 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