Example usage for java.util.zip ZipEntry isDirectory

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

Introduction

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

Prototype

public boolean isDirectory() 

Source Link

Document

Returns true if this is a directory entry.

Usage

From source file:com.google.cloud.tools.gradle.appengine.sourcecontext.SourceContextPluginIntegrationTest.java

/** Create a test project with git source context. */
public void setUpTestProject() throws IOException {
    Path buildFile = testProjectDir.getRoot().toPath().resolve("build.gradle");

    Path src = Files.createDirectory(testProjectDir.getRoot().toPath().resolve("src"));
    InputStream buildFileContent = getClass().getClassLoader()
            .getResourceAsStream("projects/sourcecontext-project/build.gradle");
    Files.copy(buildFileContent, buildFile);

    Path gitContext = testProjectDir.getRoot().toPath().resolve("gitContext.zip");
    InputStream gitContextContent = getClass().getClassLoader()
            .getResourceAsStream("projects/sourcecontext-project/gitContext.zip");
    Files.copy(gitContextContent, gitContext);

    try (ZipFile zipFile = new ZipFile(gitContext.toFile())) {
        Enumeration<? extends ZipEntry> entries = zipFile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            File entryDestination = new File(testProjectDir.getRoot(), entry.getName());
            if (entry.isDirectory()) {
                entryDestination.mkdirs();
            } else {
                entryDestination.getParentFile().mkdirs();
                InputStream in = zipFile.getInputStream(entry);
                OutputStream out = new FileOutputStream(entryDestination);
                IOUtils.copy(in, out);//from   w w w  . j a  va  2  s  .c  om
                IOUtils.closeQuietly(in);
                out.close();
            }
        }
    }

    FileUtils.delete(gitContext.toFile());

    Path webInf = testProjectDir.getRoot().toPath().resolve("src/main/webapp/WEB-INF");
    Files.createDirectories(webInf);
    File appengineWebXml = Files.createFile(webInf.resolve("appengine-web.xml")).toFile();
    Files.write(appengineWebXml.toPath(), "<appengine-web-app/>".getBytes(Charsets.UTF_8));
}

From source file:com.celements.photo.utilities.Unzip.java

/**
 * Get a List of names of all files contained in the zip file.
 * //from w ww .  jav a 2s  . co  m
 * @param zipFile byte array of the zip file.
 * @return List of all filenames (and directory names - ending with a file seperator) contained in the zip file.
 */
public List<String> getZipContentList(byte[] zipFile) {
    String fileSep = System.getProperty("file.separator");
    List<String> contentList = new ArrayList<String>();
    ZipInputStream zipStream = getZipInputStream(zipFile);

    try {
        while (zipStream.available() > 0) {
            ZipEntry entry = zipStream.getNextEntry();
            if (entry != null) {
                String fileName = entry.getName();
                if (entry.isDirectory() && !fileName.endsWith(fileSep)) {
                    fileName += fileSep;
                }
                contentList.add(fileName);
            }
        }
    } catch (IOException e) {
        mLogger.error(e);
    }

    return contentList;
}

From source file:eu.scape_project.up2ti.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/* w  ww .  j ava 2 s.  c  o m*/
 * @param destDirectory
 * @throws IOException
 */
public void unzip(String containerFileName, InputStream containerFileStream) throws IOException {
    extractDirectoryName = "/tmp/up2ti_" + 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.heliosdecompiler.helios.controller.files.OpenedFile.java

private void readQuick() {
    byte[] fileData;

    try {/*from   w w  w  . j  av a2 s  .  com*/
        fileData = Files.readAllBytes(this.target);
    } catch (IOException e) {
        this.messageHandler.handleException(Message.ERROR_IOEXCEPTION_OCCURRED.format(), e);
        return;
    }

    this.fileContents.clear();
    this.fileContents = new HashMap<>();

    try (ZipInputStream zipInputStream = new ZipInputStream(new ByteArrayInputStream(fileData))) {
        ZipEntry entry;
        while ((entry = zipInputStream.getNextEntry()) != null) {
            // todo warn about CRC
            if (!entry.isDirectory()) {
                this.fileContents.put(entry.getName(), IOUtils.toByteArray(zipInputStream));
            }
        }
    } catch (Exception ex) {
        this.messageHandler.handleException(Message.ERROR_UNKNOWN_ERROR.format(), ex);
        return;
    }

    // If files is still empty, then it's not a zip file (or something weird happened)
    if (this.fileContents.size() == 0) {
        this.fileContents.put(this.target.toString(), fileData);
    }
}

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  . j  a  va  2 s .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:ac.simons.tweetarchive.web.ArchiveHandlingController.java

/**
 * As you can see, it get's nasty here...
 * <br>/*  w  w w . j  a  va2  s . c o  m*/
 * Twitter4j doesn't offer an official way to parse Twitters JSON, so I
 * brute force my way into the twitter4j.StatusJSONImpl implementation of
 * Status.
 * <br>
 * And even if there was an official way, the JSON files inside the
 * official(!) Twitter archive differ from the API, even if they are said to
 * be identical. By the way, I'm not the only one, who
 * <a href="https://twittercommunity.com/t/why-does-twitter-json-archive-have-a-different-format-than-the-rest-api-1-1/35530">noticed
 * that</a>.
 * <br>
 * Furthermore, I didn't even bother to add error handling or tests.
 *
 * @param archive The uploaded archive
 * @return Redirect to the index
 * @throws java.io.IOException
 * @throws twitter4j.JSONException
 */
@PostMapping
public String store(@NotNull final MultipartFile archive, final RedirectAttributes redirectAttributes)
        throws IOException, JSONException {
    try (final ZipInputStream archiv = new ZipInputStream(archive.getInputStream())) {
        ZipEntry entry;
        while ((entry = archiv.getNextEntry()) != null) {
            if (!entry.getName().startsWith("data/js/tweets/") || entry.isDirectory()) {
                continue;
            }
            log.debug("Reading archive entry {}...", entry.getName());
            final BufferedReader buffer = new BufferedReader(
                    new InputStreamReader(archiv, StandardCharsets.UTF_8));

            final String content = buffer.lines().skip(1).map(l -> {
                Matcher m = PATTERN_CREATED_AT.matcher(l);
                String rv = l;
                if (m.find()) {
                    try {
                        rv = m.replaceFirst(
                                "$1\"" + DATE_FORMAT_OUT.format(DATE_FORMAT_IN.parse(m.group(2))) + "\"");
                    } catch (ParseException ex) {
                        log.warn("Unexpected date format in twitter archive", ex);
                    }
                }
                return rv;
            }).collect(Collectors.joining("")).replaceAll("\"sizes\" : \\[.+?\\],", "\"sizes\" : {},");

            final JSONArray statuses = new JSONArray(content);
            for (int i = 0; i < statuses.length(); ++i) {
                final JSONObject rawJSON = statuses.getJSONObject(i);
                // https://twitter.com/lukaseder/status/772772372990586882 ;)
                final Status status = statusFactory.create(rawJSON).as(Status.class);
                this.tweetStorageService.store(status, rawJSON.toString());
            }
        }
    }
    redirectAttributes.addFlashAttribute("message", "Done.");
    return "redirect:/upload";
}

From source file:org.apache.hadoop.hbase.util.MigrationTest.java

private void unzip(ZipInputStream zip, FileSystem dfs, Path rootDir) throws IOException {
    ZipEntry e = null;
    while ((e = zip.getNextEntry()) != null) {
        if (e.isDirectory()) {
            dfs.mkdirs(new Path(rootDir, e.getName()));
        } else {/* w w  w  . ja  v  a  2s .co  m*/
            FSDataOutputStream out = dfs.create(new Path(rootDir, e.getName()));
            byte[] buffer = new byte[4096];
            int len;
            do {
                len = zip.read(buffer);
                if (len > 0) {
                    out.write(buffer, 0, len);
                }
            } while (len > 0);
            out.close();
        }
        zip.closeEntry();
    }
}

From source file:com.silverpeas.wiki.WikiInstanciator.java

protected void createPages(File directory, String componentId) throws IOException, WikiException {
    ZipInputStream zipFile = new ZipInputStream(loader.getResourceAsStream("pages.zip"));
    ZipEntry page = zipFile.getNextEntry();
    while (page != null) {
        String pageName = page.getName();
        File newPage = new File(directory, pageName);
        if (page.isDirectory()) {
            newPage.mkdirs();//from w  ww .  j  a v  a2s.  co m
        } else {
            FileUtil.writeFile(newPage, new InputStreamReader(zipFile, "UTF-8"));
            PageDetail newPageDetail = new PageDetail();
            newPageDetail.setInstanceId(componentId);
            newPageDetail.setPageName(pageName.substring(0, pageName.lastIndexOf('.')));
            wikiDAO.createPage(newPageDetail);
            zipFile.closeEntry();
            page = zipFile.getNextEntry();
        }
    }
}

From source file:eu.diversify.disco.experiments.testing.Tester.java

private void unzipDistribution(String archiveName) throws IOException {
    String fileName = escape(archiveName);
    ZipFile zipFile = new ZipFile(fileName);
    Enumeration<? extends ZipEntry> entries = zipFile.entries();
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        if (!entry.isDirectory()) {
            File entryDestination = new File("target/", entry.getName());
            entryDestination.getParentFile().mkdirs();
            InputStream in = zipFile.getInputStream(entry);
            OutputStream out = new FileOutputStream(entryDestination);
            IOUtils.copy(in, out);//  w ww  . jav a  2s. c om
            IOUtils.closeQuietly(in);
            IOUtils.closeQuietly(out);
        }
    }
}

From source file:com.evolveum.midpoint.ninja.action.worker.ImportProducerWorker.java

@Override
public void run() {
    Log log = context.getLog();//from w w  w.j  av  a2s.  c om

    log.info("Starting import");
    operation.start();

    try (InputStream input = openInputStream()) {
        if (!options.isZip()) {
            processStream(input);
        } else {
            ZipInputStream zis = new ZipInputStream(input);
            ZipEntry entry;
            while ((entry = zis.getNextEntry()) != null) {
                if (entry.isDirectory()) {
                    continue;
                }

                if (!StringUtils.endsWith(entry.getName().toLowerCase(), ".xml")) {
                    continue;
                }

                log.info("Processing file {}", entry.getName());
                processStream(zis);
            }
        }
    } catch (IOException ex) {
        log.error("Unexpected error occurred, reason: {}", ex, ex.getMessage());
    } catch (NinjaException ex) {
        log.error(ex.getMessage(), ex);
    } finally {
        markDone();

        if (isWorkersDone()) {
            if (!operation.isFinished()) {
                operation.producerFinish();
            }
        }
    }
}