0

I want to zip two different files with different file extensions, together into one file. one file ext is .txt and other one is .amr. My zip file end up having a size 0KB XXXXXX.zip and when I extract it, it gives me a message that said file is corrupt or damaged.

I zip two different file types, Maybe that met be the case to have 0KB.

Any one with a better way of zipping files together?

  • So far i tried to check with same file extension and it works, but when I zip different file extensions, it returns a 0kb file. –  May 27 '15 at 07:37
  • Follow http://www.oracle.com/technetwork/articles/java/compress-1565076.html and if still see an issue then post the stack trace here. – Phani May 27 '15 at 15:49

2 Answers2

0

This is the method I use in my apps to make zip files... Just put files into an array and execute the makeZip method statically.

import java.util.zip.*

public class Zipper {

    /**
     * Creates a zip file from a File array <br>
     *  <br>
     * @param files to add to zipFile.
     * @param zipFile file zip to create.
     * @return compressed zip file.
     * 
     * @throws IOException in case of error.
     */
    private static synchronized File makeZip(File[] files, File zipFile) 
    {
        try {
            FileOutputStream fos = new FileOutputStream(zipFile.getAbsolutePath());
            ZipOutputStream zos = new ZipOutputStream(fos);

            for (File fileEntry : files) {
                FileInputStream fis = new FileInputStream(fileEntry);
                ZipEntry zipEntry = new ZipEntry(fileEntry.getName());
                zos.putNextEntry(zipEntry);

                byte[] bytes = new byte[1024];
                int length;
                while ((length = fis.read(bytes)) >= 0) 
                {
                    zos.write(bytes, 0, length);
                }

                zos.closeEntry();
                fis.close();

                // delete already compressed files 
                fileEntry.delete();
            }
            zos.close();
            fos.close();
        } catch (IOException e) {
            // catch exception
        }

        return zipFile;
    }
}

Use (if you put method in class Zipper):

File[] files = new File[2];
files[0] = new File("path/to/file.amr");
files[1] = new File("path/toother/file.txt");

File zipfile = Zipper.makefile(files, new File("destiny/of/file.zip"));
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
  • Glad to hear @JayStar , I still can't understand downvote, I didn't erase the answer because i'm sure code works. It's part of a library to manipulate zip's i've created for one of my projects and it's working great so far... :) – Jordi Castilla Jul 06 '15 at 13:25
  • 1
    It also worked for me, I don't know what the downvote what was it for. I got help form your source which seems to be working good. –  Jul 06 '15 at 13:31
0

I have found examples from here and from stackoverflow. They used “java.util.zip” library and they are straightforward

Community
  • 1
  • 1
Bryan
  • 1,477
  • 1
  • 21
  • 38