ZipUtil.java 6.69 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  javax.jcr.Binary
 *  javax.jcr.Node
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  org.apache.sling.api.resource.Resource
 */
package com.adobe.cq.wcm.translation.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import javax.jcr.Binary;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import org.apache.sling.api.resource.Resource;

public class ZipUtil {
    public static void zipDirectory(File directoryToZip, String strOutputFilePath) throws FileNotFoundException, IOException {
        ArrayList<File> fileList = new ArrayList<File>();
        ZipUtil.getAllFiles(directoryToZip, fileList, true);
        ZipUtil.writeZipFile(directoryToZip, fileList, strOutputFilePath);
    }

    public static void getAllFiles(File dir, List<File> fileList, boolean bAddDirectory) {
        File[] files;
        for (File file : files = dir.listFiles()) {
            if (bAddDirectory || file.isFile()) {
                fileList.add(file);
            }
            if (!file.isDirectory()) continue;
            ZipUtil.getAllFiles(file, fileList, bAddDirectory);
        }
    }

    private static void writeZipFile(File directoryToZip, List<File> fileList, String strZipFilePath) throws FileNotFoundException, IOException {
        FileOutputStream fos = new FileOutputStream(strZipFilePath);
        ZipOutputStream zos = new ZipOutputStream(fos);
        for (File file : fileList) {
            if (file.isDirectory()) continue;
            ZipUtil.addToZip(directoryToZip, file, zos);
        }
        zos.close();
        fos.close();
    }

    private static void addToZip(File directoryToZip, File file, ZipOutputStream zos) throws FileNotFoundException, IOException {
        int length;
        FileInputStream fis = new FileInputStream(file);
        String zipFilePath = file.getCanonicalPath().substring(directoryToZip.getCanonicalPath().length() + 1, file.getCanonicalPath().length());
        ZipEntry zipEntry = new ZipEntry(zipFilePath);
        zos.putNextEntry(zipEntry);
        byte[] bytes = new byte[1024];
        while ((length = fis.read(bytes)) >= 0) {
            zos.write(bytes, 0, length);
        }
        zos.closeEntry();
        fis.close();
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     * Loose catch block
     * Enabled aggressive block sorting
     * Enabled unnecessary exception pruning
     * Enabled aggressive exception aggregation
     * Lifted jumps to return sites
     */
    public static boolean isValidFile(File file) {
        boolean bValid = false;
        ZipFile zipfile = null;
        zipfile = new ZipFile(file);
        bValid = true;
        try {
            if (zipfile == null) return bValid;
            zipfile.close();
            return bValid;
        }
        catch (IOException e) {
            return bValid;
        }
        catch (ZipException e) {
            try {
                if (zipfile == null) return bValid;
                zipfile.close();
                return bValid;
            }
            catch (IOException e) {
                return bValid;
            }
            catch (IOException e2) {
                try {
                    if (zipfile == null) return bValid;
                    zipfile.close();
                    return bValid;
                }
                catch (IOException e) {
                    return bValid;
                }
                catch (Throwable throwable) {
                    try {
                        if (zipfile == null) throw throwable;
                        zipfile.close();
                        zipfile = null;
                        throw throwable;
                    }
                    catch (IOException e) {
                        // empty catch block
                    }
                    throw throwable;
                }
            }
        }
    }

    private static boolean unZipToDir(InputStream fis, String destDir, long maxFileUnzipSize) throws IOException {
        boolean bContinue = true;
        long totalFileSize = 0;
        File destDirFile = new File(destDir);
        if (!destDirFile.exists()) {
            destDirFile.mkdirs();
        }
        byte[] buffer = new byte[1024];
        ZipInputStream zis = new ZipInputStream(fis);
        ZipEntry ze = zis.getNextEntry();
        while (ze != null && bContinue) {
            String fileName = ze.getName();
            File newFile = new File(destDir + File.separator + fileName);
            if (newFile.getAbsolutePath().indexOf(destDir) == 0 && newFile.getAbsolutePath().indexOf("..") == -1) {
                new File(newFile.getParent()).mkdirs();
                if (ze.isDirectory()) {
                    newFile.mkdir();
                } else {
                    int len;
                    FileOutputStream fos = new FileOutputStream(newFile);
                    while ((len = zis.read(buffer)) > 0) {
                        fos.write(buffer, 0, len);
                    }
                    fos.close();
                    if ((totalFileSize += newFile.length()) > maxFileUnzipSize && maxFileUnzipSize > 0) {
                        bContinue = false;
                        break;
                    }
                }
            }
            zis.closeEntry();
            ze = zis.getNextEntry();
        }
        zis.closeEntry();
        zis.close();
        return bContinue;
    }

    public static boolean unZipToDir(String zipFilePath, String destDir, long maxFileUnzipSize) throws IOException {
        boolean bRetVal = false;
        FileInputStream fis = new FileInputStream(zipFilePath);
        bRetVal = ZipUtil.unZipToDir(fis, destDir, maxFileUnzipSize);
        fis.close();
        return bRetVal;
    }

    public static boolean unZipToDir(Resource zipNode, String destDir, int maxFileUnzipSize) throws RepositoryException, IOException {
        Node contentNode;
        Property prop;
        Resource zipContent = zipNode.getChild("jcr:content");
        boolean bRetVal = false;
        if (zipContent != null && (prop = (contentNode = (Node)zipContent.adaptTo(Node.class)).getProperty("jcr:data")) != null && prop.getType() == 2) {
            bRetVal = ZipUtil.unZipToDir(prop.getBinary().getStream(), destDir, (long)maxFileUnzipSize);
        }
        return bRetVal;
    }
}