Java Utililty Methods Zip Files

List of utility methods to do Zip Files

Description

The list of methods to do Zip Files are organized into topic(s).

Method

voidaddFilesToZip(File zipFile, File[] files, String[] fileNames)
add Files To Zip
File tempFile = new File(zipFile.getAbsoluteFile() + ".temp");
if (!zipFile.renameTo(tempFile)) {
    throw new RuntimeException("could not rename");
byte[] buf = new byte[1024];
ZipInputStream zin = new ZipInputStream(new FileInputStream(tempFile));
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile));
ZipEntry entry = zin.getNextEntry();
...
voidaddFileToZip(File file, String entryName, ZipOutputStream zos)
Inserts a file into a zipstream using the specified entryName.
byte[] buffer = new byte[8192];
int read = 0;
FileInputStream in = new FileInputStream(file);
ZipEntry entry = new ZipEntry(entryName);
zos.putNextEntry(entry);
while (-1 != (read = in.read(buffer))) {
    zos.write(buffer, 0, read);
in.close();
voidaddFileToZip(File file, String entryName, ZipOutputStream zOut)
Adds a file to the Zip file
if (file.isDirectory()) {
    return;
int bytesRead;
final int bufSize = 1024;
byte buf[] = new byte[bufSize];
ZipEntry zipEntry = new ZipEntry(entryName);
zipEntry.setTime(file.lastModified());
...
voidaddFileToZip(File file, String parentFolderName, ZipOutputStream zip)
Adds a specified File (that is not a folder) to the specified ZipOutputStream , the File being located in the specified parent folder.
String parent = parentFolderName == null || parentFolderName.trim().isEmpty() ? ""
        : parentFolderName.trim() + "/";
if (file.exists()) {
    zip.putNextEntry(new ZipEntry(parent + file.getName()));
    BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(file));
    byte[] buffer = new byte[16096];
    int count = 0;
    while ((count = inputStream.read(buffer)) != -1) {
...
voidaddFileToZip(File file, ZipOutputStream zos)
Adds a file to the current zip output stream
zos.putNextEntry(new ZipEntry(file.getName()));
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
byte[] bytesIn = new byte[BUFFER_SIZE];
int read = 0;
while ((read = bis.read(bytesIn)) != -1) {
    zos.write(bytesIn, 0, read);
zos.closeEntry();
...
voidaddFileToZip(File in, File parent, ZipOutputStream out)
read a file out to the zip stream
String path = in.getCanonicalPath();
String parentPath = parent.getCanonicalPath();
if (!path.startsWith(parentPath)) {
    throw new Error("not parent: " + parentPath + " of " + path);
} else {
    path = path.substring(1 + parentPath.length());
    path = path.replace('\\', '/'); 
ZipEntry entry = new ZipEntry(path);
entry.setTime(in.lastModified());
out.putNextEntry(entry);
InputStream input = null;
try {
    input = new FileInputStream(in);
    byte[] buf = new byte[1024];
    int count;
    while (0 < (count = input.read(buf, 0, buf.length))) {
        out.write(buf, 0, count);
} finally {
    if (null != input)
        input.close();
voidaddFileToZip(File root, File file, ZipOutputStream zos)
add File To Zip
FileInputStream fis = new FileInputStream(file);
String filePath = file.getCanonicalPath();
String zipFilePath = filePath.substring(root.getCanonicalPath().length() + 1, filePath.length());
ZipEntry zipEntry = new ZipEntry(zipFilePath);
zos.putNextEntry(zipEntry);
byte[] bytes = new byte[1024];
int length;
while ((length = fis.read(bytes)) >= 0) {
...
voidaddFileToZip(final File file, final String zipName, final ZipOutputStream zipout)
add File To Zip
InputStream in = null;
try {
    in = new BufferedInputStream(new FileInputStream(file));
    final ZipEntry entry = new ZipEntry(zipName);
    zipout.putNextEntry(entry);
    final byte[] buffer = new byte[2048];
    int length;
    while ((length = in.read(buffer)) != -1) {
...
voidaddFileToZip(final String path, final String srcFile, final ZipOutputStream zip, boolean flag)
Adds the file to zip.
try {
    final File folder = new File(srcFile);
    if (folder.isDirectory()) {
        addFolderToZip(path, srcFile, zip);
    } else {
        final byte[] buf = new byte[1024];
        int len;
        final FileInputStream inputStream = new FileInputStream(srcFile);
...
voidaddFileToZip(final String pathInsideZip, final File fileToZip, final ZipOutputStream outZip)
add File To Zip
if (fileToZip.isDirectory()) {
    addFolderToZip(pathInsideZip, fileToZip, outZip);
} else {
    FileInputStream in = null;
    try {
        in = new FileInputStream(fileToZip);
        outZip.putNextEntry(new ZipEntry(pathInsideZip + "/" + fileToZip.getName()));
        int count;
...