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

byte[]addToZip(byte[] zip, String file, String fileName)
Adds an entry to a zip file
InputStream zipInputStream = new ByteArrayInputStream(zip);
ZipInputStream zis = new ZipInputStream(zipInputStream);
byte[] buffer = new byte[1024];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (ZipOutputStream zos = new ZipOutputStream(baos)) {
    ZipEntry entry;
    while ((entry = zis.getNextEntry()) != null) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
...
voidaddToZip(File directoryToZip, File file, ZipOutputStream zos)
Add a file to the zip file
FileInputStream fis = null;
try {
    fis = new FileInputStream(file);
    String zipFilePath = file.getCanonicalPath().substring(directoryToZip.getCanonicalPath().length() + 1,
            file.getCanonicalPath().length());
    System.out.println("Writing '" + zipFilePath + "' to zip file");
    ZipEntry zipEntry = new ZipEntry(zipFilePath);
    zos.putNextEntry(zipEntry);
...
voidaddToZip(File f, int truncate, ZipOutputStream os, byte[] buff)
Adds file or directory to the ZIP output stream.
if (f.isFile()) {
    os.putNextEntry(new ZipEntry(f.getAbsolutePath().substring(truncate)));
    try (FileInputStream fis = new FileInputStream(f);
            BufferedInputStream bis = new BufferedInputStream(fis)) {
        int read;
        while ((read = bis.read(buff)) != -1) {
            os.write(buff, 0, read);
    os.closeEntry();
} else {
    for (File sub : f.listFiles()) {
        addToZip(sub, truncate, os, buff);
voidaddToZip(File file, ZipOutputStream out, String folder)
Zip the specified file or folder.
if (file.isDirectory()) {
    File[] files = file.listFiles();
    if (files != null) {
        for (File f : files) {
            addToZip(f, out, (folder != null ? folder + file.getName() : file.getName()) + "/");
} else {
...
voidaddToZip(File input, String entryName, ZipOutputStream zos, boolean withHidden, Set excludeEntries)
add To Zip
if (input.isHidden() && !withHidden)
    return;
if (input.isDirectory()) {
    if (entryName.length() > 0)
        entryName += "/";
    for (File f : input.listFiles())
        addToZip(f, entryName + f.getName(), zos, withHidden, excludeEntries);
} else if (input.isFile()) {
...
voidaddToZip(String path, String srcFile, ZipOutputStream zip)
add To Zip
File folder = new File(srcFile);
if (folder.isDirectory()) {
    addFolderToZip(path, srcFile, zip);
} else {
    byte[] buf = new byte[1024];
    int len;
    try {
        FileInputStream in = new FileInputStream(srcFile);
...
voidaddToZip(String path, String srcFile, ZipOutputStream zip)
add To Zip
File folder = new File(srcFile);
if (folder.isDirectory()) {
    addFolderToZip(path, srcFile, zip, true);
} else {
    byte[] buf = new byte[1024];
    int len;
    FileInputStream in = null;
    try {
...
voidaddToZip(String[] sourceFiles, ZipOutputStream output)
add To Zip
byte[] buffer = new byte[1024];
for (String file : sourceFiles) {
    File content = new File(file);
    if (content.isDirectory()) {
        File[] children = content.listFiles();
        String[] childrenPaths = new String[children.length];
        for (int i = 0; i < children.length; i++) {
            childrenPaths[i] = children[i].getAbsolutePath();
...
voidaddToZip(ZipOutputStream zos, String rootDirectoryName, String fileName)
add To Zip
byte[] buf = new byte[BUFFER_SIZE];
File d = new File(rootDirectoryName + fileName);
if (d.isDirectory()) {
    for (File file : d.listFiles()) {
        if (file.isDirectory()) {
            addToZip(zos, rootDirectoryName, fileName + file.getName() + File.separator);
            continue;
        FileInputStream fis = new FileInputStream(file.getAbsolutePath());
        ZipEntry entry = new ZipEntry(fileName + file.getName());
        zos.putNextEntry(entry);
        int len;
        while ((len = fis.read(buf)) > 0) {
            zos.write(buf, 0, len);
        zos.closeEntry();
        fis.close();
} else {
    FileInputStream fis = new FileInputStream(d.getAbsolutePath());
    ZipEntry entry = new ZipEntry(d.getName());
    zos.putNextEntry(entry);
    int len;
    while ((len = fis.read(buf)) > 0) {
        zos.write(buf, 0, len);
    zos.closeEntry();
    fis.close();
voidaddToZipFile(InputStream source, String entryName, ZipOutputStream zos)
add To Zip File
ZipEntry zipEntry = new ZipEntry(entryName);
zos.putNextEntry(zipEntry);
byte[] bytes = new byte[1024];
int length;
while ((length = source.read(bytes)) >= 0) {
    zos.write(bytes, 0, length);
zos.closeEntry();
...