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

voidaddFileToZip(int skipprefix, File file, ZipOutputStream zipper)
add File To Zip
String raw = file.getAbsolutePath().replace("\\", "/");
if (raw.length() == skipprefix) {
    if (file.isDirectory()) {
        File[] children = file.listFiles();
        if (children != null) {
            for (File child : children) {
                addFileToZip(skipprefix, child, zipper);
} else {
    String path = raw.substring(skipprefix + 1);
    if (file.isDirectory()) {
        File[] children = file.listFiles();
        if (children != null) {
            for (File child : children) {
                addFileToZip(skipprefix, child, zipper);
    } else {
        ZipEntry entry = new ZipEntry(path);
        zipper.putNextEntry(entry);
        try (FileInputStream in = new FileInputStream(file)) {
            copyStream(in, zipper);
        zipper.closeEntry();
voidaddFileToZip(String path, File srcFile, ZipOutputStream zip, boolean flag)
add File To Zip
if (flag == true) {
    zip.putNextEntry(new ZipEntry(path + "/" + srcFile.getName() + "/"));
} else {
    if (srcFile.isDirectory()) {
        addFolderToZip(path, srcFile, zip);
    } else {
        int len;
        byte[] buf = new byte[1024];
...
voidaddFileToZip(String path, File srcFile, ZipOutputStream zip, String destZipFile)
add File To Zip
if (srcFile.isDirectory()) {
    addFolderToZip(path, srcFile, zip, destZipFile);
} else if (!srcFile.getName().equals(destZipFile)) {
    byte[] buf = new byte[1024];
    int len;
    final InputStream in = new BufferedInputStream(new FileInputStream(srcFile));
    try {
        if (path.equals("/")) {
...
voidaddFileToZip(String path, String srcFile, ZipOutputStream zip)
add File To Zip
File folder = new File(srcFile);
if (folder.isDirectory()) {
    addFolderToZip(path, srcFile, zip, false);
} else {
    byte[] buf = new byte[1024];
    int len;
    FileInputStream in = new FileInputStream(srcFile);
    try {
...
voidaddFileToZip(String path, String srcFile, ZipOutputStream zip)
add File To Zip
File folder = new File(srcFile);
if (folder.isDirectory()) {
    addFolderToZip(path, srcFile, zip);
} else {
    byte[] buf = new byte[1024];
    int len;
    FileInputStream in = new FileInputStream(srcFile);
    zip.putNextEntry(new ZipEntry(path + "/" + folder.getName()));
...
voidaddFileToZip(ZipOutputStream out, InputStream in, String entry)
Adds a file into a zip archive.
byte[] buf = new byte[1024]; 
ZipEntry ze = new ZipEntry(entry);
try {
    out.putNextEntry(ze);
    int len;
    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    in.close();
    out.closeEntry();
} catch (Exception e) {
    e.printStackTrace();
voidaddFileToZip(ZipOutputStream zipOutputStream, File file, String basePath)
Add a file to the zip.
String relativeFilename = file.getCanonicalPath().substring(basePath.length() + 1,
        file.getCanonicalPath().length());
ZipEntry zipEntry = new ZipEntry(relativeFilename);
zipOutputStream.putNextEntry(zipEntry);
try (FileInputStream fileInputStream = new FileInputStream(file);) {
    byte[] inputBuffer = new byte[1024];
    int bytesRead;
    while ((bytesRead = fileInputStream.read(inputBuffer)) != -1) {
...
voidaddFileToZip(ZipOutputStream zipOutputStream, String path, byte[] bytes)
Adds a file to a .zip file
zipOutputStream.putNextEntry(new ZipEntry(path));
zipOutputStream.write(bytes, 0, bytes.length);
zipOutputStream.closeEntry();
voidaddFileToZip(ZipOutputStream zos, File file, File rootDir)
add File To Zip
if (file.isFile()) {
    File relativeFile = new File(file.getAbsolutePath().substring(rootDir.getPath().length() + 1));
    ZipEntry ze = new ZipEntry(relativeFile.getPath());
    zos.putNextEntry(ze);
    int count;
    byte data[] = new byte[BUF_SIZE];
    BufferedInputStream origin = new BufferedInputStream(new FileInputStream(file));
    while ((count = origin.read(data, 0, BUF_SIZE)) != -1) {
...
voidaddFileToZipOutputStream(File file, ZipOutputStream zipOs)
Adds the specified file to the zip output stream.
String fileName = file.getName();
try (FileInputStream fos = new FileInputStream(file)) {
    ZipEntry zipEntry = new ZipEntry(fileName);
    zipEntry.setSize(file.length());
    zipEntry.setTime(System.currentTimeMillis());
    zipOs.putNextEntry(zipEntry);
    byte[] buf = new byte[1024];
    int bytesRead;
...