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