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:com.taobao.android.tpatch.utils.JarSplitUtils.java

/**
 * jarclass//from  w w  w .jav a  2 s . co  m
 * @param inJar
 * @param removeClasses
 */
public static void removeFilesFromJar(File inJar, List<String> removeClasses) throws IOException {
    if (null == removeClasses || removeClasses.isEmpty()) {
        return;
    }
    File outJar = new File(inJar.getParentFile(), inJar.getName() + ".tmp");
    File outParentFolder = outJar.getParentFile();
    if (!outParentFolder.exists()) {
        outParentFolder.mkdirs();
    }
    FileOutputStream fos = new FileOutputStream(outJar);
    ZipOutputStream jos = new ZipOutputStream(fos);
    final byte[] buffer = new byte[8192];
    FileInputStream fis = new FileInputStream(inJar);
    ZipInputStream zis = new ZipInputStream(fis);

    try {
        // loop on the entries of the jar file package and put them in the final jar
        ZipEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            // do not take directories or anything inside a potential META-INF folder.
            if (entry.isDirectory() || !entry.getName().endsWith(".class")) {
                continue;
            }
            String name = entry.getName();
            String className = getClassName(name);
            if (removeClasses.contains(className)) {
                continue;
            }
            JarEntry newEntry;
            // Preserve the STORED method of the input entry.
            if (entry.getMethod() == JarEntry.STORED) {
                newEntry = new JarEntry(entry);
            } else {
                // Create a new entry so that the compressed len is recomputed.
                newEntry = new JarEntry(name);
            }
            // add the entry to the jar archive
            jos.putNextEntry(newEntry);

            // read the content of the entry from the input stream, and write it into the archive.
            int count;
            while ((count = zis.read(buffer)) != -1) {
                jos.write(buffer, 0, count);
            }
            // close the entries for this file
            jos.closeEntry();
            zis.closeEntry();
        }
    } finally {
        zis.close();
    }
    fis.close();
    jos.close();
    FileUtils.deleteQuietly(inJar);
    FileUtils.moveFile(outJar, inJar);
}

From source file:io.card.development.recording.Recording.java

private static Hashtable<String, byte[]> unzipFiles(InputStream recordingStream) throws IOException {
    ZipEntry zipEntry;
    ZipInputStream zis = new ZipInputStream(recordingStream);
    Hashtable<String, byte[]> fileHash = new Hashtable<String, byte[]>();
    byte[] buffer = new byte[512];

    while ((zipEntry = zis.getNextEntry()) != null) {
        if (!zipEntry.isDirectory()) {
            int read = 0;
            ByteArrayOutputStream fileStream = new ByteArrayOutputStream();

            do {/*  w  w w  . jav  a  2 s  .  c  o m*/
                read = zis.read(buffer, 0, buffer.length);
                if (read != -1) {
                    fileStream.write(buffer, 0, read);
                }
            } while (read != -1);

            byte[] fileData = fileStream.toByteArray();
            fileHash.put(zipEntry.getName(), fileData);
        }
    }

    return fileHash;
}

From source file:lucee.commons.io.compress.CompressUtil.java

private static void listZip(Resource zipFile) throws IOException {
    if (!zipFile.exists())
        throw new IOException(zipFile + " is not a existing file");

    if (zipFile.isDirectory()) {
        throw new IOException(zipFile + " is a directory");
    }/* ww w  . j a  va2  s  .c  o m*/

    ZipInputStream zis = null;
    try {
        zis = new ZipInputStream(IOUtil.toBufferedInputStream(zipFile.getInputStream()));
        ZipEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            if (!entry.isDirectory()) {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                IOUtil.copy(zis, baos, false, false);
                byte[] barr = baos.toByteArray();
                aprint.o(entry.getName() + ":" + barr.length);
            }
        }
    } finally {
        IOUtil.closeEL(zis);
    }
}

From source file:com.meltmedia.cadmium.core.util.WarUtils.java

/**
 * Adds a properties file to a war.//ww w . ja v  a  2s  .  c o  m
 * @param outZip The zip output stream to add to.
 * @param cadmiumPropertiesEntry The entry to add.
 * @param cadmiumProps The properties to store in the zip file.
 * @param newWarNames The first element of this list is used in a comment of the properties file.
 * @throws IOException
 */
public static void storeProperties(ZipOutputStream outZip, ZipEntry cadmiumPropertiesEntry,
        Properties cadmiumProps, List<String> newWarNames) throws IOException {
    ZipEntry newCadmiumEntry = new ZipEntry(cadmiumPropertiesEntry.getName());
    outZip.putNextEntry(newCadmiumEntry);
    cadmiumProps.store(outZip, "Initial git properties for " + newWarNames.get(0));
    outZip.closeEntry();
}

From source file:com.sshtools.j2ssh.authentication.UserGridCredential.java

private static void checkCACertificates(CoGProperties cogproperties) throws IOException {
    // check the directories exist and create if they don't
    String globusDir = System.getProperty("user.home") + "/.globus";
    if (!(new File(globusDir).exists())) {
        boolean success = (new File(globusDir).mkdir());
        if (!success) {
            throw new IOException("Couldn't create directory: " + globusDir);
        }/*from  ww  w.ja va2s  . c o m*/
    }
    String caCertLocations = globusDir + "/certificates";
    File caCertLocationsF = new File(caCertLocations);
    if (!caCertLocationsF.exists()) {
        boolean success = (new File(caCertLocations).mkdir());
        if (!success) {
            throw new IOException("Couldn't create directory: " + caCertLocations);
        }
    }
    if (!caCertLocationsF.isDirectory()) {
        throw new IOException("Location: " + caCertLocations + " is not a directory");
    }
    File tmp = null;
    try {
        // save the zipfile temporarily
        tmp = File.createTempFile("certificates", ".zip");
        copyFile(thisDummy.getClass().getResourceAsStream("certificates.zip"), new FileOutputStream(tmp));
        ZipFile zf = new ZipFile(tmp);
        try {
            Enumeration e = zf.entries();
            while (e.hasMoreElements()) {
                ZipEntry ze = (ZipEntry) e.nextElement();
                String name = ze.getName();
                if (!(new File(caCertLocations + File.separator + name).exists())) {
                    copyFile(zf.getInputStream(ze),
                            new FileOutputStream(new File(caCertLocations + File.separator + name)));
                }
            }
        } finally {
            if (zf != null)
                zf.close();
        }
    } catch (IOException e) {
        throw new IOException("Couldn't load certificates... " + e);
    } finally {
        // delete temp file
        if (tmp != null) {
            if (tmp.exists())
                tmp.delete();
        }
    }
}

From source file:com.sldeditor.test.unit.tool.vector.VectorToolTest.java

/**
 * Unzip a zip file containing shp file.
 *
 * @param zipFilePath the zip file path/*from w w w  .  j a  v  a  2s  . c om*/
 * @param destDirectory the dest directory
 * @throws IOException Signals that an I/O exception has occurred.
 */
private static void unzip(String zipFilePath, String destDirectory) throws IOException {
    File destDir = new File(destDirectory);
    if (!destDir.exists()) {
        destDir.mkdir();
    }
    ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
    ZipEntry entry = zipIn.getNextEntry();
    // iterates over entries in the zip file
    while (entry != null) {
        String filePath = destDirectory + File.separator + entry.getName();
        if (!entry.isDirectory()) {
            // if the entry is a file, extracts it
            extractFile(zipIn, filePath);
        } else {
            // if the entry is a directory, make the directory
            File dir = new File(filePath);
            dir.mkdir();
        }
        zipIn.closeEntry();
        entry = zipIn.getNextEntry();
    }
    zipIn.close();
}

From source file:com.taobao.android.tpatch.utils.JarSplitUtils.java

/**
 * ?jarentry/*from   ww  w.  j  a  va2s.c  o  m*/
 *
 * @param jarFile
 * @return
 */
public static List<String> getZipEntries(File jarFile) throws IOException {
    List<String> entries = new ArrayList<String>();
    FileInputStream fis = new FileInputStream(jarFile);
    ZipInputStream zis = new ZipInputStream(fis);
    try {
        // loop on the entries of the jar file package and put them in the final jar
        ZipEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            // do not take directories or anything inside a potential META-INF folder.
            if (entry.isDirectory()) {
                continue;
            }
            String name = entry.getName();
            entries.add(name);
        }
    } finally {
        zis.close();
    }
    return entries;
}

From source file:com.mhs.hboxmaintenanceserver.utils.Utils.java

/**
 * Unzip it//from   w  w  w . j av  a  2  s  .  c  o m
 *
 * @param zipFile input zip file
 * @param outputFolder
 * @throws java.io.FileNotFoundException
 */
public static void unzip(String zipFile, String outputFolder) throws FileNotFoundException, IOException {
    byte[] buffer = new byte[1024];

    try (ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile))) {
        ZipEntry ze = zis.getNextEntry();

        while (ze != null) {

            String fileName = ze.getName();
            File newFile = new File(outputFolder + File.separator + fileName);
            if (ze.isDirectory()) {
                newFile.mkdirs();
            } else {
                try (FileOutputStream fos = new FileOutputStream(newFile)) {
                    int len;
                    while ((len = zis.read(buffer)) > 0) {
                        fos.write(buffer, 0, len);
                    }
                    fos.close();
                }
            }
            ze = zis.getNextEntry();
        }
        zis.closeEntry();
        zis.close();
    }
}

From source file:org.zilverline.service.CollectionManagerImpl.java

/**
 * unZips a given zip file into cache directory with derived name. e.g. c:\temp\file.zip wil be unziiped into
 * [cacheDir]\file_zip\./*ww  w. jav  a2  s  .  c  o m*/
 * 
 * @param sourceZipFile the ZIP file to be unzipped
 * @param thisCollection the collection whose cache and contenDir is used
 * 
 * @return File (new) directory containing zip file
 */
public static File unZip(final File sourceZipFile, final FileSystemCollection thisCollection) {
    // specify buffer size for extraction
    final int aBUFFER = 2048;
    File unzipDestinationDirectory = null;
    ZipFile zipFile = null;
    FileOutputStream fos = null;
    BufferedOutputStream dest = null;
    BufferedInputStream bis = null;

    try {
        // Specify destination where file will be unzipped
        unzipDestinationDirectory = file2CacheDir(sourceZipFile, thisCollection);
        log.info("unzipping " + sourceZipFile + " into " + unzipDestinationDirectory);
        // Open Zip file for reading
        zipFile = new ZipFile(sourceZipFile, ZipFile.OPEN_READ);
        // Create an enumeration of the entries in the zip file
        Enumeration zipFileEntries = zipFile.entries();
        while (zipFileEntries.hasMoreElements()) {
            // grab a zip file entry
            ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
            String currentEntry = entry.getName();
            log.debug("Extracting: " + entry);
            File destFile = new File(unzipDestinationDirectory, currentEntry);
            // grab file's parent directory structure
            File destinationParent = destFile.getParentFile();
            // create the parent directory structure if needed
            destinationParent.mkdirs();
            // extract file if not a directory
            if (!entry.isDirectory()) {
                bis = new BufferedInputStream(zipFile.getInputStream(entry));
                int currentByte;
                // establish buffer for writing file
                byte[] data = new byte[aBUFFER];
                // write the current file to disk
                fos = new FileOutputStream(destFile);
                dest = new BufferedOutputStream(fos, aBUFFER);
                // read and write until last byte is encountered
                while ((currentByte = bis.read(data, 0, aBUFFER)) != -1) {
                    dest.write(data, 0, currentByte);
                }
                dest.flush();
                dest.close();
                bis.close();
            }
        }
        zipFile.close();
        // delete the zip file if it is in the cache, we don't need to store
        // it, since we've extracted the contents
        if (FileUtils.isIn(sourceZipFile, thisCollection.getCacheDirWithManagerDefaults())) {
            sourceZipFile.delete();
        }
    } catch (Exception e) {
        log.error("Can't unzip: " + sourceZipFile, e);
    } finally {
        try {
            if (fos != null) {
                fos.close();
            }
            if (dest != null) {
                dest.close();
            }
            if (bis != null) {
                bis.close();
            }
        } catch (IOException e1) {
            log.error("Error closing files", e1);
        }
    }

    return unzipDestinationDirectory;
}

From source file:com.meltmedia.cadmium.core.util.WarUtils.java

/**
 * Writes a xml document to a zip file with an entry specified by the jbossWeb parameter.
 * @param outZip The zip output stream to write to.
 * @param jbossWeb The zip entry to add to the zip file.
 * @param doc The xml DOM document to write to the zip file.
 * @throws IOException /*from  ww  w.  j  ava2 s. co m*/
 * @throws TransformerFactoryConfigurationError
 * @throws TransformerConfigurationException
 * @throws TransformerException
 */
public static void storeXmlDocument(ZipOutputStream outZip, ZipEntry jbossWeb, Document doc) throws IOException,
        TransformerFactoryConfigurationError, TransformerConfigurationException, TransformerException {
    jbossWeb = new ZipEntry(jbossWeb.getName());
    outZip.putNextEntry(jbossWeb);

    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer();

    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

    DOMSource source = new DOMSource(doc);
    StreamResult result = new StreamResult(outZip);
    transformer.transform(source, result);

    outZip.closeEntry();
}