Unable to create zip file

From my below code, a zip file is getting created, but with 0 bytes.

Could anyone please help me with my code to get the files and folders zipped.

void doCreate(File dir)throws FileNotFoundException, IOException

{

String[] filenames=new String[]{dir.toString()};

//String[] filenames=dir.list(); //if I give like this it is creating an error

byte[] buf =newbyte[1024];

try

{

// Create the ZIP file

String outFilename ="C:\\zip\\outfile.zip";

ZipOutputStream out =new ZipOutputStream(new FileOutputStream(outFilename));

for (int i=0; i<filenames.length; i++)

{

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

}

// Complete the ZIP file

out.close();

}

catch (IOException e)

{

}

}

[2208 byte] By [Simmya] at [2007-11-26 18:51:53]
# 1

The reason why you run into errors when using String[] filenames=dir.list();

is that it returns only the file names (without the path). In your loop you try to add a file that is not in the directory where your app is running, so it doesn't find it.

You could use

File[] files=dir.list();

...

for (...) {

FileInputStream in = new FileInputStream(files[i]);

out.putNextEntry(new ZipEntry(files[i].getAbsolutePath()));

...

}

In this case you get all files in your zip-file with their absolute path.

If you want the path relative to the specified directory, you'll have to use that relative path in out.putNextEntry(new ZipEntry("relativePathHere"));

Peetzorea at 2007-7-9 6:25:54 > top of Java-index,Java Essentials,New To Java...
# 2

Thanks for the reply. As per your directions I changed my code as shown below. I am able to zip if only files are there inside a folder. I need to zip a folder consisting of both files and folders, but it's creating the following error

Cannot open file: it does not appear to be a valid archive.

If you downloaded this file, try downloading the file again.

If I do the zipping of the folder consisting of both files and folder manually, it is getting zipped. So my winzip is working fine.

Please do tell me what more modifications to do in my code.

void doCreate(File dir) throws FileNotFoundException, IOException

{

File[] files=dir.listFiles();

byte[] buf = new byte[2048];

try

{

// Create the ZIP file

String outFilename = "C:\\zip\\outfile.zip";

ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outFilename));

for (int i=0; i<files.length; i++)

{

FileInputStream in = new FileInputStream(files[i]);

// Add ZIP entry to output stream.

out.putNextEntry(new ZipEntry(files[i].getAbsolutePath()));

// 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)

{

}

}

Simmya at 2007-7-9 6:25:54 > top of Java-index,Java Essentials,New To Java...
# 3
This might help: http://www.devx.com/tips/Tip/14049
Peetzorea at 2007-7-9 6:25:54 > top of Java-index,Java Essentials,New To Java...
# 4

I took the code from the site mentioned by you.

Inside my selected folder, I have many files and folders.

From my below code, I am able to zip all the files and the files inside the first folder only.

But I am unable to zip the files that are inside other folders too.

For this, could you please tell me where to modify in my code

void doCreateZipFile(String dir) throws FileNotFoundException, IOException

{

ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(dir+".zip"));

zipDir(dir, zos);

zos.close();

}

public void zipDir(String dir2zip, ZipOutputStream zos)

{

try

{

zipDir = new File(dir2zip);

String[] dirList = zipDir.list();

byte[] readBuffer = new byte[2156];

int bytesIn = 0;

for(int i=0; i<dirList.length; i++)

{

File f = new File(zipDir, dirList[i]);

if(f.isDirectory())

{

String filePath = f.getPath();

zipDir(filePath, zos);

continue;

}

FileInputStream fis = new FileInputStream(f);

ZipEntry anEntry = new ZipEntry(f.getPath());

zos.putNextEntry(anEntry);

while((bytesIn = fis.read(readBuffer)) != -1)

{

zos.write(readBuffer, 0, bytesIn);

}

fis.close();

}

}

catch(Exception e)

{

//handle exception

}

}

>

Simmya at 2007-7-9 6:25:54 > top of Java-index,Java Essentials,New To Java...
# 5

Check this out this works!!!

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

import java.io.FileNotFoundException;

import java.io.File;

public class ZipFile {

public ZipFile() {

super();

// TODO Auto-generated constructor stub

}

void doCreate(File dir)

{

//String[] filenames=new String[]{dir.toString()};

String[] filenames= dir.list();//does no give error

File [] files = dir.listFiles(); //Get Files instead of file names

byte[] buf = new byte[1024];

try

{

// Create the ZIP file

String outFilename = "D:\\zip\\outfile.zip";

ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outFilename));

for (int i=0; i<filenames.length; i++)

{

FileInputStream in = new FileInputStream(files[i].getAbsolutePath()); // pass absolute path

// Add ZIP entry to output stream.

out.putNextEntry(new ZipEntry(files[i].getAbsolutePath())); // pass absolute path

// 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(FileNotFoundException fe)

{

fe.printStackTrace();

}

catch (IOException e)

{

e.printStackTrace();

}

}

public static void main(String args[])

{

ZipFile zip = new ZipFile();

zip.doCreate(new File("D:\\app"));

}

}

cheers!!

2713a at 2007-7-9 6:25:54 > top of Java-index,Java Essentials,New To Java...
# 6

Instead of doing a dir.list() and getting only the list of file names, you need to do a listFiles() and get an array of Files, then use the file.getAbsolutePath() to get the name of the file (including the full path) to create the zip entry. Here is a working program that does what you want. Read thru it and understand what it is doing.

public class Forum {

private static ZipOutputStream zip;

public static void main(String[] args) throws FileNotFoundException, IOException {

File dir = new File("c:\\Testing\\junk");

zip = new ZipOutputStream(new FileOutputStream("C:\\Zipped.zip"));

doCreate(dir, zip);

//Complete the ZIP file

zip.close();

}

private static void doCreate(File dir, ZipOutputStream zip) throws FileNotFoundException, IOException

{

System.out.println(dir.toString());

File [] files = dir.listFiles();

try

{

// Create the ZIP file

for (int i=0; i<files.length; i++)

{

File f = new File(files[i].getAbsolutePath());

System.out.println(f.getAbsolutePath());

if(!(f.isDirectory()))

{

// Add ZIP entry to output stream.

zip.putNextEntry(new ZipEntry(f.getAbsolutePath()));

FileInputStream fin = new FileInputStream(f.getAbsoluteFile());

byte[] buf = new byte[4096];

int len = 4096;

while((len = fin.read(buf))>0)

{

zip.write(buf,0,len);

}

fin.close();

// Complete the entry

zip.closeEntry();

}

else

{

doCreate(f,zip);

}

}

}

catch (IOException e)

{

}

}

}

~Tim

SomeoneElsea at 2007-7-9 6:25:54 > top of Java-index,Java Essentials,New To Java...
# 7
Thanks a lot for the help. My program is working fine.
Simmya at 2007-7-9 6:25:54 > top of Java-index,Java Essentials,New To Java...