How to create a zip file of multiple image files
Asked Answered
R

2

14

I am trying to create a zip file of multiple image files. I have succeeded in creating the zip file of all the images but somehow all the images have been hanged to 950 bytes. I don't know whats going wrong here and now I can't open the images were compressed into that zip file.

Here is my code. Can anyone let me know what's going here?

String path="c:\\windows\\twain32";
File f=new File(path);
f.mkdir();
File x=new File("e:\\test");
x.mkdir();
byte []b;
String zipFile="e:\\test\\test.zip";
FileOutputStream fout=new FileOutputStream(zipFile);
ZipOutputStream zout=new ZipOutputStream(new BufferedOutputStream(fout));


File []s=f.listFiles();
for(int i=0;i<s.length;i++)
{
    b=new byte[(int)s[i].length()];
    FileInputStream fin=new FileInputStream(s[i]);
    zout.putNextEntry(new ZipEntry(s[i].getName()));
    int length;
    while((length=fin.read())>0)
    {
        zout.write(b,0,length);
    }
    zout.closeEntry();
    fin.close();
}
zout.close();
Rightwards answered 14/5, 2013 at 15:19 Comment(0)
S
11

Change this:

while((length=fin.read())>0)

to this:

while((length=fin.read(b, 0, 1024))>0)

And set buffer size to 1024 bytes:

b=new byte[1024];
Stamata answered 14/5, 2013 at 15:23 Comment(2)
thanks it works very well You have solved the problem thanks bro thanks a lot....:DRightwards
if you think this answer is appropriate, accept it. the same applies to all your questions you asked in the pastStamata
E
17

This is my zip function I always use for any file structures:

public static File zip(List<File> files, String filename) {
    File zipfile = new File(filename);
    // Create a buffer for reading the files
    byte[] buf = new byte[1024];
    try {
        // create the ZIP file
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));
        // compress the files
        for(int i=0; i<files.size(); i++) {
            FileInputStream in = new FileInputStream(files.get(i).getCanonicalName());
            // add ZIP entry to output stream
            out.putNextEntry(new ZipEntry(files.get(i).getName()));
            // 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();
        return zipfile;
    } catch (IOException ex) {
        System.err.println(ex.getMessage());
    }
    return null;
}
Exaggeration answered 14/5, 2013 at 15:36 Comment(3)
Thankd for this sample which works well, except that I had to change this line: FileInputStream in = new FileInputStream(files.get(i).getCanonicalName());Apothecium
Hi - you're right, in order to keep the folder structure, it's better to use *.getCanonicalName() I have adapted that to my answer - thank you.Exaggeration
Hi. I am wondering (i) why we are not clearing the buffer after out.write statement, wouldn't buffer get overflowed ? (ii) what should be the buffer size kept in general for size of files less than 1 MB ?Cattle
S
11

Change this:

while((length=fin.read())>0)

to this:

while((length=fin.read(b, 0, 1024))>0)

And set buffer size to 1024 bytes:

b=new byte[1024];
Stamata answered 14/5, 2013 at 15:23 Comment(2)
thanks it works very well You have solved the problem thanks bro thanks a lot....:DRightwards
if you think this answer is appropriate, accept it. the same applies to all your questions you asked in the pastStamata

© 2022 - 2024 — McMap. All rights reserved.