Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 217
  • Last Modified:

adding a file to a zip file

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
Jaziar
Asked:
Jaziar
  • 2
1 Solution
 
contactkarthiCommented:
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
 
ysnkyCommented:
0
 
contactkarthiCommented:
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 Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now