Search in sources :

Example 1 with ZipEntry

use of java.util.zip.ZipEntry in project hadoop by apache.

the class FileUtil method unZip.

/**
   * Given a File input it will unzip the file in a the unzip directory
   * passed as the second parameter
   * @param inFile The zip file as input
   * @param unzipDir The unzip directory where to unzip the zip file.
   * @throws IOException
   */
public static void unZip(File inFile, File unzipDir) throws IOException {
    Enumeration<? extends ZipEntry> entries;
    ZipFile zipFile = new ZipFile(inFile);
    try {
        entries = zipFile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            if (!entry.isDirectory()) {
                InputStream in = zipFile.getInputStream(entry);
                try {
                    File file = new File(unzipDir, entry.getName());
                    if (!file.getParentFile().mkdirs()) {
                        if (!file.getParentFile().isDirectory()) {
                            throw new IOException("Mkdirs failed to create " + file.getParentFile().toString());
                        }
                    }
                    OutputStream out = new FileOutputStream(file);
                    try {
                        byte[] buffer = new byte[8192];
                        int i;
                        while ((i = in.read(buffer)) != -1) {
                            out.write(buffer, 0, i);
                        }
                    } finally {
                        out.close();
                    }
                } finally {
                    in.close();
                }
            }
        }
    } finally {
        zipFile.close();
    }
}
Also used : ZipFile(java.util.zip.ZipFile) GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) BufferedOutputStream(java.io.BufferedOutputStream) JarOutputStream(java.util.jar.JarOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Example 2 with ZipEntry

use of java.util.zip.ZipEntry in project hadoop by apache.

the class JarFinder method zipDir.

private static void zipDir(File dir, String relativePath, ZipOutputStream zos, boolean start) throws IOException {
    String[] dirList = dir.list();
    for (String aDirList : dirList) {
        File f = new File(dir, aDirList);
        if (!f.isHidden()) {
            if (f.isDirectory()) {
                if (!start) {
                    ZipEntry dirEntry = new ZipEntry(relativePath + f.getName() + "/");
                    zos.putNextEntry(dirEntry);
                    zos.closeEntry();
                }
                String filePath = f.getPath();
                File file = new File(filePath);
                zipDir(file, relativePath + f.getName() + "/", zos, false);
            } else {
                String path = relativePath + f.getName();
                if (!path.equals(JarFile.MANIFEST_NAME)) {
                    ZipEntry anEntry = new ZipEntry(path);
                    copyToZipStream(f, anEntry, zos);
                }
            }
        }
    }
}
Also used : ZipEntry(java.util.zip.ZipEntry) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 3 with ZipEntry

use of java.util.zip.ZipEntry in project hadoop by apache.

the class JarFinder method jarDir.

public static void jarDir(File dir, String relativePath, ZipOutputStream zos) throws IOException {
    Preconditions.checkNotNull(relativePath, "relativePath");
    Preconditions.checkNotNull(zos, "zos");
    // by JAR spec, if there is a manifest, it must be the first entry in the
    // ZIP.
    File manifestFile = new File(dir, JarFile.MANIFEST_NAME);
    ZipEntry manifestEntry = new ZipEntry(JarFile.MANIFEST_NAME);
    if (!manifestFile.exists()) {
        zos.putNextEntry(manifestEntry);
        new Manifest().write(new BufferedOutputStream(zos));
        zos.closeEntry();
    } else {
        copyToZipStream(manifestFile, manifestEntry, zos);
    }
    zos.closeEntry();
    zipDir(dir, relativePath, zos, true);
    zos.close();
}
Also used : ZipEntry(java.util.zip.ZipEntry) Manifest(java.util.jar.Manifest) JarFile(java.util.jar.JarFile) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 4 with ZipEntry

use of java.util.zip.ZipEntry in project hadoop by apache.

the class TestLocalJobSubmission method makeJar.

private Path makeJar(Path p) throws IOException {
    FileOutputStream fos = new FileOutputStream(new File(p.toString()));
    JarOutputStream jos = new JarOutputStream(fos);
    ZipEntry ze = new ZipEntry("test.jar.inside");
    jos.putNextEntry(ze);
    jos.write(("inside the jar!").getBytes());
    jos.closeEntry();
    jos.close();
    return p;
}
Also used : FileOutputStream(java.io.FileOutputStream) ZipEntry(java.util.zip.ZipEntry) JarOutputStream(java.util.jar.JarOutputStream) File(java.io.File)

Example 5 with ZipEntry

use of java.util.zip.ZipEntry in project hbase by apache.

the class JarFinder method zipDir.

private static void zipDir(File dir, String relativePath, ZipOutputStream zos, boolean start) throws IOException {
    String[] dirList = dir.list();
    for (String aDirList : dirList) {
        File f = new File(dir, aDirList);
        if (!f.isHidden()) {
            if (f.isDirectory()) {
                if (!start) {
                    ZipEntry dirEntry = new ZipEntry(relativePath + f.getName() + "/");
                    zos.putNextEntry(dirEntry);
                    zos.closeEntry();
                }
                String filePath = f.getPath();
                File file = new File(filePath);
                zipDir(file, relativePath + f.getName() + "/", zos, false);
            } else {
                String path = relativePath + f.getName();
                if (!path.equals(JarFile.MANIFEST_NAME)) {
                    ZipEntry anEntry = new ZipEntry(path);
                    copyToZipStream(f, anEntry, zos);
                }
            }
        }
    }
}
Also used : ZipEntry(java.util.zip.ZipEntry) JarFile(java.util.jar.JarFile) File(java.io.File)

Aggregations

ZipEntry (java.util.zip.ZipEntry)1367 ZipFile (java.util.zip.ZipFile)479 File (java.io.File)469 IOException (java.io.IOException)361 ZipOutputStream (java.util.zip.ZipOutputStream)321 ZipInputStream (java.util.zip.ZipInputStream)300 InputStream (java.io.InputStream)282 FileOutputStream (java.io.FileOutputStream)278 FileInputStream (java.io.FileInputStream)270 Test (org.junit.Test)124 BufferedInputStream (java.io.BufferedInputStream)122 JarFile (java.util.jar.JarFile)122 BufferedOutputStream (java.io.BufferedOutputStream)99 ByteArrayOutputStream (java.io.ByteArrayOutputStream)97 ArrayList (java.util.ArrayList)84 ByteArrayInputStream (java.io.ByteArrayInputStream)78 OutputStream (java.io.OutputStream)67 JarOutputStream (java.util.jar.JarOutputStream)59 Path (java.nio.file.Path)56 Enumeration (java.util.Enumeration)56