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