Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

zip file is created with 0 bytes

807599Feb 16 2007 — edited Feb 16 2007
In my below code, I am trying to zip a folder containing files and folders. But the zip file is getting created with 0 bytes.

Could anyone please tell me what is wrong in my coding.
private void jButton5ZipActionPerformed(java.awt.event.ActionEvent evt)
{	
	File sgm=new File(jTextField1Text);//jTextField1Text is the filename I am selecting from the jTextField
	File tempXML=new File(sgm.getParent());
	String[] children = tempXML.list();
	for(int i=0;i<children.length;i++)
	{
		File renamingFile=new File(tempXML,children);
CreateZipFile czf=new CreateZipFile();
try
{
czf.doCreate(renamingFile);
}
catch (FileNotFoundException ex)
{
ex.printStackTrace();
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
}
==============================
void doCreate(File dir) throws FileNotFoundException, IOException 
{
	String[] filenames=new String[]{dir.toString()};
	byte[] buf = new byte[1024];
	try 
	{
		// Create the ZIP file
		String outFilename = "C:\\outfile.zip";
		ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outFilename));
		for (int i=0; i<filenames.length; i++) 
		{
			FileInputStream in = new FileInputStream(filenames);

// 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();
}

// Complete the ZIP file
out.close();
}
catch (IOException e)
{
}
}

Message was edited by:
Simmy
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 16 2007
Added on Feb 16 2007
11 comments
1,149 views