Example usage for java.util.zip ZipFile getName

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

Introduction

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

Prototype

public String getName() 

Source Link

Document

Returns the path name of the ZIP file.

Usage

From source file:org.zgis.wps.swat.AnnotatedSwatRunnerAlgorithm.java

private URI extractToTemp(final ZipFile zipFile, final String filename) throws IOException {
    final File tempFile;
    final ZipEntry entry;

    tempFile = new File(ExecutionContextFactory.getContext().getTempDirectoryPath()
            + System.getProperty("file.separator") + filename);
    tempFile.getParentFile().mkdirs();/*  ww w.  java2s . c om*/
    entry = zipFile.getEntry(filename);

    if (entry == null) {
        throw new FileNotFoundException("cannot find file: " + filename + " in archive: " + zipFile.getName());
    }

    try (OutputStream fileStream = new FileOutputStream(tempFile);
            InputStream zipStream = zipFile.getInputStream(entry)) {
        final byte[] buf;
        int i;

        buf = new byte[1024];
        i = 0;

        while ((i = zipStream.read(buf)) != -1) {
            fileStream.write(buf, 0, i);
        }
    }

    return (tempFile.toURI());
}

From source file:org.ambraproject.article.service.IngestArchiveProcessorTest.java

@Test(dataProviderClass = SampleArticleData.class, dataProvider = "sampleAssets", dependsOnMethods = "testProcessArticle", alwaysRun = true)
public void testParseWithSecondaryObjects(ZipFile archive, List<ArticleAsset> expectedAssets)
        throws ArchiveProcessException {
    Document articleXml = ingestArchiveProcessor.extractArticleXml(archive);
    Article result = ingestArchiveProcessor.processArticle(archive, articleXml);
    assertNotNull(result, "Returned null article");
    assertNotNull(result.getAssets(), "returned null asset list");
    assertEquals(result.getAssets().size(), expectedAssets.size(), "returned incorrect number of assets");

    for (int i = 0; i < result.getAssets().size(); i++) {
        compareAssets(result.getAssets().get(i), expectedAssets.get(i));
    }//from   w w  w  .  java2  s.  c  o  m
    String archiveName = archive.getName().contains(File.separator)
            ? archive.getName().substring(archive.getName().lastIndexOf(File.separator) + 1)
            : archive.getName();
    assertEquals(result.getArchiveName(), archiveName, "Article didn't have archive name set correctly");
}

From source file:org.apache.maven.archetype.common.DefaultArchetypeArtifactManager.java

private Reader getDescriptorReader(ZipFile zipFile, String descriptor) throws IOException {
    ZipEntry entry = searchEntry(zipFile, descriptor);

    if (entry == null) {
        return null;
    }/* www  .ja  v  a  2s .  c  o m*/

    InputStream is = zipFile.getInputStream(entry);

    if (is == null) {
        throw new IOException("The " + descriptor + " descriptor cannot be read in " + zipFile.getName() + ".");
    }

    return ReaderFactory.newReader(is, ReaderFactory.UTF_8);
}

From source file:org.ambraproject.article.service.IngesterImpl.java

/**
 * Process files from the archive and store them to the {@link FileStoreService}
 *
 * @param archive - the archive being ingested
 * @param doi/*from w w  w  . jav a 2s . c om*/
 * @throws java.io.IOException - if there's a problem reading from the zip file
 * @throws org.ambraproject.filestore.FileStoreException
 *                             - if there's a problem writing files to the file store
 */
private void storeFiles(final ZipFile archive, String doi) throws IOException, FileStoreException {
    log.info("Removing existing files (if any) for {}", doi);

    try {
        documentManagementService.removeFromFileSystem(doi);
    } catch (Exception e) {
        throw new FileStoreException("Error removing existing files from the file store", e);
    }

    log.info("Storing files from archive {} to the file store", archive.getName());
    Enumeration<? extends ZipEntry> entries = archive.entries();

    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        if (!entry.getName().equalsIgnoreCase("manifest.dtd")) {
            InputStream inputStream = null;
            OutputStream outputStream = null;
            try {
                inputStream = archive.getInputStream(entry);
                outputStream = fileStoreService.getFileOutStream(FSIDMapper.zipToFSID(doi, entry.getName()),
                        entry.getSize());
                fileStoreService.copy(inputStream, outputStream);
            } finally {
                if (inputStream != null) {
                    try {
                        inputStream.close();
                    } catch (IOException e) {
                        log.warn("Error closing input stream while writing files", e);
                    }
                }
                if (outputStream != null) {
                    try {
                        outputStream.close();
                    } catch (IOException e) {
                        log.warn("Error closing output stream while writing files", e);
                    }
                }
            }
        }
    }
    log.info("Finished storing files from archive {}", archive.getName());
}

From source file:org.opencms.importexport.CmsImportVersion5.java

/**
 * @see org.opencms.importexport.I_CmsImport#importResources(org.opencms.file.CmsObject, java.lang.String, org.opencms.report.I_CmsReport, java.io.File, java.util.zip.ZipFile, org.dom4j.Document)
 * /*  w w  w .ja v a2  s .c  o m*/
 * @deprecated use {@link #importData(CmsObject, I_CmsReport, CmsImportParameters)} instead
 */
public void importResources(CmsObject cms, String importPath, I_CmsReport report, File importResource,
        ZipFile importZip, Document docXml) throws CmsImportExportException {

    CmsImportParameters params = new CmsImportParameters(
            importResource != null ? importResource.getAbsolutePath() : importZip.getName(), importPath, false);

    try {
        importData(cms, report, params);
    } catch (CmsXmlException e) {
        throw new CmsImportExportException(e.getMessageContainer(), e);
    }
}

From source file:org.teiid.jboss.rest.RestASMBasedWebArchiveBuilder.java

private void writeDirectoryEntry(ZipOutputStream out, String name) throws IOException {
    ZipFile zipFile = getZipFile(name);
    Enumeration<?> en = zipFile.entries();
    while (en.hasMoreElements()) {
        ZipEntry entry = (ZipEntry) en.nextElement();
        if (!entry.isDirectory()) {
            writeEntry(entry.getName(), out, IOUtils.toByteArray(zipFile.getInputStream(entry)));
        }/* w  ww. jav  a 2  s .  c o m*/
    }
    FileUtils.remove(new File(zipFile.getName()));
}

From source file:no.uio.medicine.virsurveillance.parsers.CSVsGBDdata.java

public void parse(String deathFolder) throws IOException {
    File f = new File(deathFolder);
    Runtime runtime = Runtime.getRuntime();
    if (f.isDirectory()) {
        String[] filesInDir = f.list();

        for (String fil : filesInDir) {
            if (fil.endsWith(".zip")) {
                ZipFile zipFile = new ZipFile(deathFolder + "/" + fil);

                Enumeration<? extends ZipEntry> entries = zipFile.entries();

                while (entries.hasMoreElements()) {
                    System.out.println(
                            "Used memory: " + (runtime.totalMemory() - runtime.freeMemory()) / (1024 * 1024)
                                    + " Free memory: " + (runtime.freeMemory()) / (1024 * 1024));

                    ZipEntry entry = entries.nextElement();
                    InputStream stream = zipFile.getInputStream(entry);
                    BufferedReader br = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
                    CSVParser parser = CSVFormat.RFC4180.withDelimiter(',').withIgnoreEmptyLines().withHeader()
                            .parse(br);//w ww.ja  v a 2  s. c  om

                    List<CSVRecord> records = parser.getRecords();
                    System.out.println("Reading records: " + zipFile.getName() + "/" + entry);
                    /*for (int i=0;i<records.size();i++) {
                    CSVRecord csvRecord = records.get(i);*/
                    for (CSVRecord csvRecord : records) {
                        if (csvRecord.isMapped("age_group_id")) { //age group 22 corresponds to all ages
                            if (csvRecord.get("age_group_id").equalsIgnoreCase("22")) {
                                String location = null;
                                String year = null;
                                String sex = null;
                                String cause = null;
                                String number = null;
                                String metric = null;

                                if (csvRecord.isMapped("location_code")) {
                                    location = csvRecord.get("location_code");
                                }
                                if (csvRecord.isMapped("year")) {
                                    year = csvRecord.get("year");
                                }
                                if (csvRecord.isMapped("sex_id")) { //1=male, 2 = female
                                    if (csvRecord.get("sex_id").equalsIgnoreCase(("1"))) {
                                        sex = "m";
                                    } else if (csvRecord.get("sex_id").equalsIgnoreCase("2")) {
                                        sex = "f";
                                    }
                                }
                                if (csvRecord.isMapped("cause_name")) {
                                    cause = csvRecord.get("cause_name");
                                }
                                if (csvRecord.isMapped("mean")) {
                                    number = csvRecord.get("mean");
                                }
                                if (csvRecord.isMapped("metric") && csvRecord.isMapped("unit")) {
                                    metric = csvRecord.get("metric") + "-" + csvRecord.get("unit");
                                }

                                if (location != null && year != null && sex != null && cause != null
                                        && number != null && metric != null) {
                                    try {
                                        sqlM.addSanitaryIssueToCountry(location, year, sex, cause, metric,
                                                number);
                                    } catch (SQLException ex) {

                                        Logger.getLogger(CSVsGBDdata.class.getName()).log(Level.SEVERE, null,
                                                ex);
                                    }
                                }

                            }

                        }

                    }

                    parser.close();

                    stream.close();
                    br.close();
                }
                zipFile.close();
            }
        }
    } else {
        System.out.println("Not a directory");
    }
}

From source file:org.sakaiproject.archive.impl.SiteZipper.java

/**
 * Unzip a zip file into the unzip directory. Return the NAME of the archive so we can then merge from it. The merger knows the base dir.
 * @param zipFilePath      path to ZIP file
 * @param m_unzipPath      unzip dir for zips
 * @return/*ww w .  j a  v a  2 s.  c o  m*/
 * @throws IOException
 */
public String unzipArchive(String zipFilePath, String m_unzipPath) throws IOException {

    log.debug("zipFilePath: " + zipFilePath);

    ZipFile zipFile = new ZipFile(zipFilePath);
    Enumeration<? extends ZipEntry> entries = zipFile.entries();
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();

        //destination file from zip. Straight into the normal archive directory
        File dest = new File(m_unzipPath, entry.getName());
        log.debug("Dest: " + dest.getAbsolutePath());

        if (entry.isDirectory()) {
            //create dir
            dest.mkdir();
        } else {
            //extract contents
            InputStream in = zipFile.getInputStream(entry);
            OutputStream out = new FileOutputStream(dest);
            IOUtils.copy(in, out);
            IOUtils.closeQuietly(in);
            IOUtils.closeQuietly(out);
        }
    }

    //get original filename, remove timestamp, add -archive
    String unzippedArchivePath = StringUtils.substringAfterLast(
            StringUtils.substringBeforeLast(zipFile.getName(), "-") + "-archive", File.separator);

    log.debug("unzippedArchivePath: " + unzippedArchivePath);

    return unzippedArchivePath;

}

From source file:org.opencms.importexport.CmsImportVersion2.java

/**
 * @see org.opencms.importexport.I_CmsImport#importResources(org.opencms.file.CmsObject, java.lang.String, org.opencms.report.I_CmsReport, java.io.File, java.util.zip.ZipFile, org.dom4j.Document)
 * /*from  w  w w . j a  va 2 s .  c o  m*/
 * @deprecated use {@link #importData(CmsObject, I_CmsReport, CmsImportParameters)} instead
 */
public void importResources(CmsObject cms, String importPath, I_CmsReport report, File importResource,
        ZipFile importZip, Document docXml) throws CmsImportExportException {

    CmsImportParameters params = new CmsImportParameters(
            importResource != null ? importResource.getAbsolutePath() : importZip.getName(), importPath, true);

    try {
        importData(cms, report, params);
    } catch (CmsXmlException e) {
        throw new CmsImportExportException(e.getMessageContainer(), e);
    }
}

From source file:org.ambraproject.article.service.XslIngestArchiveProcessor.java

@Override
public String extractStrikingImageURI(ZipFile archive) throws ArchiveProcessException {
    String strkImg = "";
    try {//from www  .j  a v a 2s.com
        Document manifest = extractXml(archive, "MANIFEST.xml");
        strkImg = xPathUtil.evaluate(manifest, "//object[@strkImage='True']/@uri");
    } catch (Exception e) {
        log.info("No Striking image selected in manifest.xml. " + archive.getName());
    }
    return strkImg;
}