Example usage for java.util.zip ZipEntry getName

List of usage examples for java.util.zip ZipEntry getName

Introduction

In this page you can find the example usage for java.util.zip ZipEntry getName.

Prototype

public String getName() 

Source Link

Document

Returns the name of the entry.

Usage

From source file:io.druid.java.util.common.CompressionUtils.java

/**
 * Unzip the pulled file to an output directory. This is only expected to work on zips with lone files, and is not intended for zips with directory structures.
 *
 * @param pulledFile The file to unzip/*  ww w  . j  av a2s .  c o m*/
 * @param outDir     The directory to store the contents of the file.
 *
 * @return a FileCopyResult of the files which were written to disk
 *
 * @throws IOException
 */
public static FileUtils.FileCopyResult unzip(final File pulledFile, final File outDir) throws IOException {
    if (!(outDir.exists() && outDir.isDirectory())) {
        throw new ISE("outDir[%s] must exist and be a directory", outDir);
    }
    log.info("Unzipping file[%s] to [%s]", pulledFile, outDir);
    final FileUtils.FileCopyResult result = new FileUtils.FileCopyResult();
    try (final ZipFile zipFile = new ZipFile(pulledFile)) {
        final Enumeration<? extends ZipEntry> enumeration = zipFile.entries();
        while (enumeration.hasMoreElements()) {
            final ZipEntry entry = enumeration.nextElement();
            final File outFile = new File(outDir, entry.getName());

            validateZipOutputFile(pulledFile.getCanonicalPath(), outFile, outDir);

            result.addFiles(FileUtils.retryCopy(new ByteSource() {
                @Override
                public InputStream openStream() throws IOException {
                    return new BufferedInputStream(zipFile.getInputStream(entry));
                }
            }, outFile, FileUtils.IS_EXCEPTION, DEFAULT_RETRY_COUNT).getFiles());
        }
    }
    return result;
}

From source file:azkaban.common.utils.Utils.java

public static void unzip(ZipFile source, File dest) throws IOException {
    Enumeration<?> entries = source.entries();
    while (entries.hasMoreElements()) {
        ZipEntry entry = (ZipEntry) entries.nextElement();
        File newFile = new File(dest, entry.getName());
        if (entry.isDirectory()) {
            newFile.mkdirs();/*from   w w w .j av  a  2  s .c om*/
        } else {
            newFile.getParentFile().mkdirs();
            InputStream src = source.getInputStream(entry);
            OutputStream output = new BufferedOutputStream(new FileOutputStream(newFile));
            IOUtils.copy(src, output);
            src.close();
            output.close();
        }
    }
}

From source file:utils.APIImporter.java

/**
 * function to unzip the imported folder
 * @param zipFile zip file path//  w  w  w .  j  a v a2 s  . c o  m
 * @param outputFolder folder to copy the zip content
 * @throws APIImportException
 */
private static void unzipFolder(String zipFile, String outputFolder) throws APIImportException {
    byte[] buffer = new byte[1024];

    try {
        ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
        //get the zipped file list entry
        ZipEntry ze = zis.getNextEntry();
        FileOutputStream fos = null;
        while (ze != null) {
            String fileName = ze.getName();
            File newFile = new File(outputFolder + File.separator + fileName);

            //create all non exists folders
            //else you will hit FileNotFoundException for compressed folder
            new File(newFile.getParent()).mkdirs();
            fos = new FileOutputStream(newFile);
            int len;
            while ((len = zis.read(buffer)) > 0) {
                fos.write(buffer, 0, len);
            }
            fos.close();
            ze = zis.getNextEntry();
        }
        zis.closeEntry();
        IOUtils.closeQuietly(zis);
        IOUtils.closeQuietly(fos);
        //            ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
        //            //get the zipped file list entry
        //            ZipEntry ze = zis.getNextEntry();
        //            FileOutputStream fos = null;
        //            while(ze!=null){
        //                String fileName = ze.getName();
        //                File newFile = new File(outputFolder + File.separator + fileName);
        //                //create all non exists folders
        //                //else it hit FileNotFoundException for compressed folder
        //                boolean directoryStatus = new File(newFile.getParent()).mkdirs();
        //                while (directoryStatus){
        //                     fos = new FileOutputStream(newFile);
        //                    int len;
        //                    while ((len = zis.read(buffer)) > 0) {
        //                        fos.write(buffer, 0, len);
        //                    }
        //                    ze = zis.getNextEntry();
        //                }
        //            }
        //            zis.closeEntry();
        //            IOUtils.closeQuietly(fos);
        //            IOUtils.closeQuietly(zis);
    } catch (IOException e) {
        String errorMsg = "Cannot extract the zip entries ";
        log.error(errorMsg, e);
        throw new APIImportException(errorMsg, e);
    }
}

From source file:com.asakusafw.bulkloader.testutil.UnitTestUtil.java

/**
 * Create file list from Zip file.//w  w  w.j  av a 2  s.  c  om
 * @param originalZipFile source Zip file
 * @param targetFileList target file list file
 */
public static void createFileList(File originalZipFile, File targetFileList) throws IOException {
    ZipFile zip = new ZipFile(originalZipFile);
    try {
        FileOutputStream output = new FileOutputStream(targetFileList);
        try {
            FileList.Writer writer = FileList.createWriter(output, true);
            Enumeration<? extends ZipEntry> entries = zip.entries();
            while (entries.hasMoreElements()) {
                ZipEntry next = entries.nextElement();
                InputStream input = zip.getInputStream(next);
                try {
                    OutputStream target = writer.openNext(FileList.content(next.getName().replace('\\', '/')));
                    try {
                        IOUtils.pipingAndClose(input, target);
                    } finally {
                        target.close();
                    }
                } finally {
                    input.close();
                }
            }
            writer.close();
        } finally {
            output.close();
        }
    } finally {
        zip.close();
    }
}

From source file:com.matze5800.paupdater.Functions.java

public static void addFilesToExistingZip(File zipFile, File[] files) throws IOException {
    File tempFile = new File(Environment.getExternalStorageDirectory() + "/pa_updater", "temp_kernel.zip");
    tempFile.delete();/*  w ww . j a  va  2  s.com*/

    boolean renameOk = zipFile.renameTo(tempFile);
    if (!renameOk) {
        throw new RuntimeException(
                "could not rename the file " + zipFile.getAbsolutePath() + " to " + tempFile.getAbsolutePath());
    }
    byte[] buf = new byte[1024];

    ZipInputStream zin = new ZipInputStream(new FileInputStream(tempFile));
    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile));

    ZipEntry entry = zin.getNextEntry();
    while (entry != null) {
        String name = entry.getName();
        boolean notInFiles = true;
        for (File f : files) {
            if (f.getName().equals(name)) {
                notInFiles = false;
                break;
            }
        }
        if (notInFiles) {

            out.putNextEntry(new ZipEntry(name));

            int len;
            while ((len = zin.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
        }
        entry = zin.getNextEntry();
    }
    zin.close();

    for (int i = 0; i < files.length; i++) {
        InputStream in = new FileInputStream(files[i]);
        out.putNextEntry(new ZipEntry(files[i].getName()));

        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        out.closeEntry();
        in.close();
    }
    out.close();
    tempFile.delete();
}

From source file:com.t3.persistence.FileUtil.java

public static void unzip(ZipInputStream in, File destDir) throws IOException {
    if (in == null)
        throw new IOException("input stream cannot be null");

    // Prepare destination
    destDir.mkdirs();//  w ww.ja va2  s .  c o m
    String absDestDir = destDir.getAbsolutePath() + File.separator;

    // Pull out the files
    ZipEntry entry = null;

    while ((entry = in.getNextEntry()) != null) {
        if (entry.isDirectory())
            continue;

        // Prepare file destination
        String path = absDestDir + entry.getName();
        File file = new File(path);
        file.getParentFile().mkdirs();

        try (OutputStream out = new FileOutputStream(file)) {
            IOUtils.copy(in, out);
        }
        in.closeEntry();
    }
}

From source file:org.adl.samplerte.server.LMSPackageHandler.java

/****************************************************************************
**
** Method:   findManifest()// w w  w . j a  v  a 2 s  .c om
** Input:  String zipFileName  --  The name of the zip file to be used
** Output:   Boolean  --  Signifies whether or not the manifest was found.
**
** Description: This method takes in the name of a zip file and tries to 
**              locate the imsmanifest.xml file
**              
*****************************************************************************/
public static boolean findManifest(String zipFileName) {
    if (_Debug) {
        System.out.println("***********************");
        System.out.println("in findManifest()      ");
        System.out.println("***********************\n");
    }

    boolean rtn = false;

    try {
        ZipInputStream in = new ZipInputStream(new FileInputStream(zipFileName));

        ZipEntry entry;
        int flag = 0;

        while ((flag != 1) && (in.available() != 0)) {
            entry = in.getNextEntry();

            if (in.available() != 0) {
                if ((entry.getName()).equalsIgnoreCase("imsmanifest.xml")) {
                    if (_Debug) {
                        System.out.println("Located manifest.... returning true");
                    }
                    flag = 1;
                    rtn = true;
                }
            }
        }

        in.close();
    } catch (IOException e) {
        if (_Debug) {
            System.out.println("IO Exception Caught: " + e);
        }
    }
    return rtn;
}

From source file:net.firejack.platform.core.utils.ArchiveUtils.java

/**
 * @param zipFile//from   w w  w .  j  a va2s . c om
 * @param files
 * @throws java.io.IOException
 */
public static void addFilesToZip(File zipFile, Map<String, File> files) throws IOException {
    // get a temp file
    File tempFile = File.createTempFile(zipFile.getName(), null);
    // delete it, otherwise you cannot rename your existing zip to it.
    FileUtils.deleteQuietly(tempFile);

    boolean renameOk = zipFile.renameTo(tempFile);
    if (!renameOk) {
        throw new RuntimeException(
                "could not rename the file " + zipFile.getAbsolutePath() + " to " + tempFile.getAbsolutePath());
    }

    ZipInputStream zin = new ZipInputStream(new FileInputStream(tempFile));
    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile));

    ZipEntry entry = zin.getNextEntry();
    while (entry != null) {
        String name = entry.getName();
        boolean notInFiles = true;
        for (Map.Entry<String, File> e : files.entrySet()) {
            if (e.getKey().equals(name)) {
                notInFiles = false;
                break;
            }
        }
        if (notInFiles) {
            out.putNextEntry(new ZipEntry(name));
            IOUtils.copy(zin, out);
        }
        entry = zin.getNextEntry();
    }
    // Close the streams
    zin.close();
    // Compress the files
    for (Map.Entry<String, File> e : files.entrySet()) {
        InputStream in = new FileInputStream(e.getValue());
        // Add ZIP entry to output stream.
        out.putNextEntry(new ZipEntry(e.getKey()));
        // Transfer bytes from the file to the ZIP file

        IOUtils.copy(in, out);
        // Complete the entry
        out.closeEntry();
        IOUtils.closeQuietly(in);
    }
    // Complete the ZIP file
    IOUtils.closeQuietly(out);
    FileUtils.deleteQuietly(tempFile);
}

From source file:net.firejack.platform.core.utils.ArchiveUtils.java

/**
 * @param zipFile/* w  w w  . j a  va 2 s  . c  o m*/
 * @param files
 * @throws java.io.IOException
 */
public static void addStreamsToZip(File zipFile, Map<String, InputStream> files) throws IOException {
    // get a temp file
    File tempFile = File.createTempFile(zipFile.getName(), null);
    // delete it, otherwise you cannot rename your existing zip to it.
    FileUtils.deleteQuietly(tempFile);

    boolean renameOk = zipFile.renameTo(tempFile);
    if (!renameOk) {
        throw new RuntimeException(
                "could not rename the file " + zipFile.getAbsolutePath() + " to " + tempFile.getAbsolutePath());
    }

    ZipInputStream zin = new ZipInputStream(new FileInputStream(tempFile));
    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile));

    ZipEntry entry = zin.getNextEntry();
    while (entry != null) {
        String name = entry.getName();
        boolean notInFiles = true;
        for (Map.Entry<String, InputStream> e : files.entrySet()) {
            if (e.getKey().equals(name)) {
                notInFiles = false;
                break;
            }
        }
        if (notInFiles) {
            out.putNextEntry(new ZipEntry(name));
            IOUtils.copy(zin, out);
        }
        entry = zin.getNextEntry();
    }
    // Close the streams
    zin.close();
    // Compress the files
    for (Map.Entry<String, InputStream> e : files.entrySet()) {
        InputStream in = e.getValue();
        // Add ZIP entry to output stream.
        out.putNextEntry(new ZipEntry(e.getKey()));
        // Transfer bytes from the file to the ZIP file

        IOUtils.copy(in, out);
        // Complete the entry
        out.closeEntry();
        IOUtils.closeQuietly(in);
    }
    // Complete the ZIP file
    IOUtils.closeQuietly(out);
    FileUtils.deleteQuietly(tempFile);
}

From source file:io.druid.java.util.common.CompressionUtils.java

/**
 * Unzip from the input stream to the output directory, using the entry's file name as the file name in the output directory.
 * The behavior of directories in the input stream's zip is undefined.
 * If possible, it is recommended to use unzip(ByteStream, File) instead
 *
 * @param in     The input stream of the zip data. This stream is closed
 * @param outDir The directory to copy the unzipped data to
 *
 * @return The FileUtils.FileCopyResult containing information on all the files which were written
 *
 * @throws IOException/* w w  w  .j ava 2s.c  om*/
 */
public static FileUtils.FileCopyResult unzip(InputStream in, File outDir) throws IOException {
    try (final ZipInputStream zipIn = new ZipInputStream(in)) {
        final FileUtils.FileCopyResult result = new FileUtils.FileCopyResult();
        ZipEntry entry;
        while ((entry = zipIn.getNextEntry()) != null) {
            final File file = new File(outDir, entry.getName());

            validateZipOutputFile("", file, outDir);

            NativeIO.chunkedCopy(zipIn, file);

            result.addFile(file);
            zipIn.closeEntry();
        }
        return result;
    }
}