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:net.famzangl.minecraft.minebot.settings.MinebotDirectoryCreator.java

public File createDirectory(File dir) throws IOException {
    CodeSource src = MinebotDirectoryCreator.class.getProtectionDomain().getCodeSource();
    if (src != null) {
        URL jar = src.getLocation();
        if (jar.getFile().endsWith("class") && !jar.getFile().contains(".jar!")) {
            System.out.println("WARNING: Using the dev directory for settings.");
            // We are in a dev enviroment.
            return new File(new File(jar.getFile()).getParentFile(), "minebot");
        }/*  w ww  .j a  v  a 2s .  c o m*/
        if (dir.isFile()) {
            dir.delete();
        }

        dir.mkdirs();
        ZipInputStream zip = new ZipInputStream(jar.openStream());
        try {
            ZipEntry zipEntry;
            while ((zipEntry = zip.getNextEntry()) != null) {
                String name = zipEntry.getName();
                if (name.startsWith(BASE)) {
                    String[] localName = name.substring(BASE.length()).split("/");
                    File currentDir = dir;
                    for (int i = 0; i < localName.length; i++) {
                        currentDir = new File(currentDir, localName[i]);
                        currentDir.mkdir();
                    }
                    File copyTo = new File(currentDir, localName[localName.length - 1]);
                    extract(zip, copyTo);
                }
            }
        } finally {
            zip.close();
        }
        return dir;
    } else {
        throw new IOException("Could not find minebot directory to extract.");
    }
}

From source file:org.openmrs.module.clinicalsummary.web.controller.upload.UploadSummariesController.java

public void validate(final String filename, final String password) throws Exception {
    String encryptedFilename = StringUtils.join(Arrays.asList(filename, TaskConstants.FILE_TYPE_ENCRYPTED),
            ".");
    ZipFile encryptedFile = new ZipFile(new File(TaskUtils.getEncryptedOutputPath(), encryptedFilename));

    byte[] initVector = null;
    byte[] encryptedSampleBytes = null;
    Enumeration<? extends ZipEntry> entries = encryptedFile.entries();
    while (entries.hasMoreElements()) {
        ZipEntry zipEntry = entries.nextElement();
        String zipEntryName = zipEntry.getName();
        if (zipEntryName.endsWith(TaskConstants.FILE_TYPE_SECRET)) {
            InputStream inputStream = encryptedFile.getInputStream(zipEntry);
            initVector = FileCopyUtils.copyToByteArray(inputStream);
            if (initVector.length != IV_SIZE) {
                throw new Exception("Secret file is corrupted or invalid secret file are being used.");
            }//from   w  ww  .ja  v a  2 s .c o m
        } else if (zipEntryName.endsWith(TaskConstants.FILE_TYPE_SAMPLE)) {
            InputStream inputStream = encryptedFile.getInputStream(zipEntry);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            FileCopyUtils.copy(inputStream, baos);
            encryptedSampleBytes = baos.toByteArray();
        }
    }

    if (initVector != null && encryptedSampleBytes != null) {
        SecretKeyFactory factory = SecretKeyFactory.getInstance(TaskConstants.SECRET_KEY_FACTORY);
        KeySpec spec = new PBEKeySpec(password.toCharArray(), password.getBytes(), 1024, 128);
        SecretKey tmp = factory.generateSecret(spec);
        // generate the secret key
        SecretKey secretKey = new SecretKeySpec(tmp.getEncoded(), TaskConstants.KEY_SPEC);
        // create the cipher
        Cipher cipher = Cipher.getInstance(TaskConstants.CIPHER_CONFIGURATION);
        cipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(initVector));
        // decrypt the sample
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(encryptedSampleBytes);
        CipherInputStream cipherInputStream = new CipherInputStream(byteArrayInputStream, cipher);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        FileCopyUtils.copy(cipherInputStream, baos);

        String sampleText = baos.toString();
        if (!sampleText.contains("This is sample text")) {
            throw new Exception("Upload parameters incorrect!");
        }
    }
}

From source file:ch.astina.hesperid.web.services.dbmigration.impl.ClasspathMigrationResolver.java

public Set<Migration> resolve() {
    Set<Migration> migrations = new HashSet<Migration>();

    try {//w  w  w  .ja  v  a 2  s.c  o  m
        Enumeration<URL> enumeration = getClass().getClassLoader().getResources(classPath);

        while (enumeration.hasMoreElements()) {

            URL url = enumeration.nextElement();

            if (url.toExternalForm().startsWith("jar:")) {
                String file = url.getFile();

                file = file.substring(0, file.indexOf("!"));
                file = file.substring(file.indexOf(":") + 1);

                InputStream is = new FileInputStream(file);

                ZipInputStream zip = new ZipInputStream(is);

                ZipEntry ze;

                while ((ze = zip.getNextEntry()) != null) {
                    if (!ze.isDirectory() && ze.getName().startsWith(classPath)
                            && ze.getName().endsWith(".sql")) {

                        Resource r = new UrlResource(getClass().getClassLoader().getResource(ze.getName()));

                        String version = versionExtractor.extractVersion(r.getFilename());
                        migrations.add(migrationFactory.create(version, r));
                    }
                }
            } else {
                File file = new File(url.getFile());

                for (String s : file.list()) {
                    Resource r = new UrlResource(getClass().getClassLoader().getResource(classPath + "/" + s));

                    String version = versionExtractor.extractVersion(r.getFilename());
                    migrations.add(migrationFactory.create(version, r));
                }
            }
        }
    } catch (Exception e) {
        logger.error("Error while resolving migrations", e);
    }

    return migrations;
}

From source file:asciidoc.maven.plugin.tools.ZipHelper.java

public void unzipEntry(ZipFile zipfile, ZipEntry zipEntry, File outputDir) throws IOException {
    if (zipEntry.isDirectory()) {
        createDir(new File(outputDir, zipEntry.getName()));
        return;//from   w w w.j a v  a2  s  .c om
    }
    File outputFile = new File(outputDir, zipEntry.getName());
    if (!outputFile.getParentFile().exists()) {
        createDir(outputFile.getParentFile());
    }

    if (this.log.isDebugEnabled())
        this.log.debug("Extracting " + zipEntry);
    BufferedInputStream inputStream = new BufferedInputStream(zipfile.getInputStream(zipEntry));
    BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile));
    try {
        IOUtils.copy(inputStream, outputStream);
    } finally {
        outputStream.close();
        inputStream.close();
    }

    if ((outputFile != null)
            && (!outputFile.isDirectory() && FileHelper.getFileExtension(outputFile).equalsIgnoreCase("py"))) {
        outputFile.setExecutable(true);
    }
}

From source file:net.sourceforge.jweb.maven.mojo.InWarMinifyMojo.java

private String getEntryType(ZipEntry e) {
    String result = null;/*from w w w.j  a  v a2 s.  c  o m*/
    String fname = e.getName();
    int index = fname.lastIndexOf(".");
    if (index > 0) {
        String type = fname.substring(index + 1);
        result = type.toLowerCase();
    }
    return result;
}

From source file:eu.scape_project.archiventory.container.ZipContainer.java

/**
 * Extracts a zip file specified by the zipFilePath to a directory specified by
 * destDirectory (will be created if does not exists)
 * @param containerFileName//from   ww  w.java  2s.  c  o  m
 * @param destDirectory
 * @throws IOException
 */
public void unzip(String containerFileName, InputStream containerFileStream) throws IOException {
    extractDirectoryName = "/tmp/archiventory_" + RandomStringUtils.randomAlphabetic(10) + "/";
    File destDir = new File(extractDirectoryName);
    destDir.mkdir();
    String subDestDirStr = extractDirectoryName + containerFileName + "/";
    File subDestDir = new File(subDestDirStr);
    subDestDir.mkdir();
    ZipInputStream zipIn = new ZipInputStream(containerFileStream);
    ZipEntry entry = zipIn.getNextEntry();
    while (entry != null) {
        String filePath = subDestDirStr + entry.getName();
        if (!entry.isDirectory()) {
            extractFile(zipIn, filePath);
        } else {
            File dir = new File(filePath);
            dir.mkdir();
        }
        zipIn.closeEntry();
        entry = zipIn.getNextEntry();
    }
    zipIn.close();
}

From source file:com.marklogic.mapreduce.ZipEntryInputStream.java

public boolean hasNext() {
    try {/*from ww  w  . j  av a  2s  .com*/
        ZipEntry entry;
        while ((entry = zipIn.getNextEntry()) != null) {
            if (entry.getSize() > 0) {
                entryName = entry.getName();
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Zip entry name: " + entryName);
                }
                return true;
            }
        }
        return false;
    } catch (IOException e) {
        LOG.error("Error getting next zip entry from " + fileName, e);
        return false;
    }
}

From source file:com.streamsets.datacollector.bundles.TestSupportBundleManager.java

private ZipFile zipFile(List<String> bundles, BundleType bundleType) throws Exception {
    InputStream bundleStream = manager.generateNewBundle(bundles, bundleType).getInputStream();
    File outputFile = File.createTempFile("test-support-bundle", ".zip");
    outputFile.deleteOnExit();/*  w  w  w.  j  a va2 s  . c  om*/

    try (FileOutputStream outputStream = new FileOutputStream(outputFile)) {
        IOUtils.copy(bundleStream, outputStream);
    }

    ZipFile zipFile = new ZipFile(outputFile);
    Enumeration<? extends ZipEntry> entries = zipFile.entries();
    LOG.debug("Archive content:");
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        LOG.debug("Entry {}", entry.getName());
    }

    return zipFile;
}

From source file:ccm.pay2spawn.types.MusicType.java

@Override
public void printHelpList(File configFolder) {
    musicFolder = new File(configFolder, "music");
    if (musicFolder.mkdirs()) {
        new Thread(new Runnable() {
            @Override// ww  w .j ava 2  s.c  om
            public void run() {
                try {
                    File zip = new File(musicFolder, "music.zip");
                    FileUtils.copyURLToFile(new URL(Constants.MUSICURL), zip);
                    ZipFile zipFile = new ZipFile(zip);
                    Enumeration<? extends ZipEntry> entries = zipFile.entries();
                    while (entries.hasMoreElements()) {
                        ZipEntry entry = entries.nextElement();
                        File entryDestination = new File(musicFolder, entry.getName());
                        entryDestination.getParentFile().mkdirs();
                        InputStream in = zipFile.getInputStream(entry);
                        OutputStream out = new FileOutputStream(entryDestination);
                        IOUtils.copy(in, out);
                        IOUtils.closeQuietly(in);
                        IOUtils.closeQuietly(out);
                    }
                    zipFile.close();
                    zip.delete();
                } catch (IOException e) {
                    Pay2Spawn.getLogger()
                            .warn("Error downloading music file. Get from github and unpack yourself please.");
                    e.printStackTrace();
                }
            }
        }, "Pay2Spawn music download and unzip").start();
    }
}

From source file:be.fedict.eid.applet.service.signer.ooxml.OOXMLURIDereferencer.java

private InputStream findDataInputStream(String uri) throws IOException {
    String entryName;/*from   www .jav a2s .  c  om*/
    if (uri.startsWith("/")) {
        entryName = uri.substring(1); // remove '/'
    } else {
        entryName = uri.toString();
    }
    if (-1 != entryName.indexOf("?")) {
        entryName = entryName.substring(0, entryName.indexOf("?"));
    }
    LOG.debug("ZIP entry name: " + entryName);

    InputStream ooxmlInputStream;
    if (null != this.ooxmlDocument) {
        ooxmlInputStream = new ByteArrayInputStream(this.ooxmlDocument);
    } else {
        ooxmlInputStream = this.ooxmlUrl.openStream();
    }
    ZipArchiveInputStream ooxmlZipInputStream = new ZipArchiveInputStream(ooxmlInputStream, "UTF8", true, true);
    ZipEntry zipEntry;
    while (null != (zipEntry = ooxmlZipInputStream.getNextZipEntry())) {
        if (zipEntry.getName().equals(entryName)) {
            return ooxmlZipInputStream;
        }
    }
    return null;
}