Example usage for java.util.zip ZipInputStream ZipInputStream

List of usage examples for java.util.zip ZipInputStream ZipInputStream

Introduction

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

Prototype

public ZipInputStream(InputStream in) 

Source Link

Document

Creates a new ZIP input stream.

Usage

From source file:com.alibaba.antx.util.ZipUtil.java

/**
 * zip/*w  w w . jav  a  2s .  com*/
 *
 * @param istream   ?
 * @param todir     
 * @param overwrite ?
 * @throws IOException Zip?
 */
public static void expandFile(InputStream istream, File todir, boolean overwrite) throws IOException {
    ZipInputStream zipStream = null;

    if (!(istream instanceof BufferedInputStream)) {
        istream = new BufferedInputStream(istream, 8192);
    }

    try {
        zipStream = new ZipInputStream(istream);

        ZipEntry zipEntry = null;

        while ((zipEntry = zipStream.getNextEntry()) != null) {
            extractFile(todir, zipStream, zipEntry, overwrite);
        }

        log.info("expand complete");
    } finally {
        if (zipStream != null) {
            try {
                zipStream.close();
            } catch (IOException e) {
            }
        }
    }
}

From source file:com.eviware.soapui.integration.exporter.ProjectExporter.java

public static void unpackageAll(String archive, String path) {
    try {//ww  w .  j  av  a  2  s . c  om
        BufferedOutputStream dest = null;
        FileInputStream fis = new FileInputStream(archive);
        ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
        ZipEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            int count;
            byte data[] = new byte[BUFFER];
            // write the files to the disk
            FileOutputStream fos = new FileOutputStream(path + File.separator + entry.getName());
            dest = new BufferedOutputStream(fos, BUFFER);
            while ((count = zis.read(data, 0, BUFFER)) != -1) {
                dest.write(data, 0, count);
            }
            dest.flush();
            dest.close();
        }
        zis.close();
    } catch (Exception e) {
        SoapUI.logError(e);
    }
}

From source file:com.thoughtworks.go.server.service.ArtifactsService.java

public boolean saveFile(File dest, InputStream stream, boolean shouldUnzip, int attempt) {
    String destPath = dest.getAbsolutePath();
    try {/* w  ww. j a  va  2 s  .c  om*/
        LOGGER.trace("Saving file [{}]", destPath);
        if (shouldUnzip) {
            zipUtil.unzip(new ZipInputStream(stream), dest);
        } else {
            systemService.streamToFile(stream, dest);
        }
        LOGGER.trace("File [{}] saved.", destPath);
        return true;
    } catch (IOException e) {
        final String message = format("Failed to save the file to: [%s]", destPath);
        if (attempt < GoConstants.PUBLISH_MAX_RETRIES) {
            LOGGER.warn(message, e);
        } else {
            LOGGER.error(message, e);
        }
        return false;
    } catch (IllegalPathException e) {
        final String message = format("Failed to save the file to: [%s]", destPath);
        LOGGER.error(message, e);
        return false;
    }
}

From source file:com.mcleodmoores.mvn.natives.UnpackDependenciesMojo.java

private void gatherNames(final Artifact artifact, final Map<String, Set<Artifact>> names)
        throws MojoFailureException {
    getLog().debug("Scanning " + ArtifactUtils.key(artifact));
    check(artifact, (new IOCallback<InputStream, Boolean>(open(artifact)) {

        @Override//w w w  .  ja  v a2 s .c  o m
        protected Boolean apply(final InputStream input) throws IOException {
            final ZipInputStream zip = new ZipInputStream(new BufferedInputStream(input));
            ZipEntry entry;
            while ((entry = zip.getNextEntry()) != null) {
                gatherName(artifact, entry.getName(), names);
            }
            return Boolean.TRUE;
        }

    }).call(new MojoLoggingErrorCallback(this)));
}

From source file:ac.simons.tweetarchive.web.ArchiveHandlingController.java

/**
 * As you can see, it get's nasty here...
 * <br>//from  www . java  2s  . c om
 * 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:net.ftb.util.FileUtils.java

/**
 * Extracts given zip to given location//from  w ww . j  a v  a2s . co  m
 * @param zipLocation - the location of the zip to be extracted
 * @param outputLocation - location to extract to
 */
public static void extractZipTo(String zipLocation, String outputLocation) {
    ZipInputStream zipinputstream = null;
    try {
        byte[] buf = new byte[1024];
        zipinputstream = new ZipInputStream(new FileInputStream(zipLocation));
        ZipEntry zipentry = zipinputstream.getNextEntry();
        while (zipentry != null) {
            String entryName = zipentry.getName();
            int n;
            if (!zipentry.isDirectory() && !entryName.equalsIgnoreCase("minecraft")
                    && !entryName.equalsIgnoreCase(".minecraft") && !entryName.equalsIgnoreCase("instMods")) {
                new File(outputLocation + File.separator + entryName).getParentFile().mkdirs();
                FileOutputStream fileoutputstream = new FileOutputStream(
                        outputLocation + File.separator + entryName);
                while ((n = zipinputstream.read(buf, 0, 1024)) > -1) {
                    fileoutputstream.write(buf, 0, n);
                }
                fileoutputstream.close();
            }
            zipinputstream.closeEntry();
            zipentry = zipinputstream.getNextEntry();
        }
    } catch (Exception e) {
        Logger.logError("Error while extracting zip", e);
        backupExtract(zipLocation, outputLocation);
    } finally {
        try {
            zipinputstream.close();
        } catch (IOException e) {
        }
    }
}

From source file:com.seer.datacruncher.services.ftp.FTPPollJobProcessor.java

@Override
public void process(Exchange exchange) throws Exception {
    String result = "";
    GenericFile file = (GenericFile) exchange.getIn().getBody();
    Message message = exchange.getOut();
    String inputFileName = file.getFileName();
    Map<String, byte[]> resultMap = new HashMap<String, byte[]>();
    if (isValidFileName(inputFileName)) {
        long schemaId = getSchemaIdUsingFileName(inputFileName);
        long userId = getUserIdFromFileName(inputFileName);
        if (!usersDao.isUserAssoicatedWithSchema(userId, schemaId)) {
            result = "User not authorized";
        } else {/*from   w  w w  .  j  a v  a  2s  . c  o  m*/
            resultMap.put(inputFileName, file.getBody().toString().getBytes());
            SchemaEntity schemaEntity = schemasDao.find(schemaId);
            if (schemaEntity == null) {
                result = "No schema found in database with Id [" + schemaId + "]";
            } else {
                if (inputFileName.endsWith(FileExtensionType.ZIP.getAbbreviation())) {
                    // Case 1: When user upload a Zip file - All ZIP entries should be validate one by one   
                    ZipInputStream inStream = null;
                    try {
                        inStream = new ZipInputStream(new ByteArrayInputStream(resultMap.get(inputFileName)));
                        ZipEntry entry;
                        while (!(isStreamClose(inStream)) && (entry = inStream.getNextEntry()) != null) {
                            if (!entry.isDirectory()) {
                                DatastreamsInput datastreamsInput = new DatastreamsInput();
                                datastreamsInput.setUploadedFileName(entry.getName());
                                byte[] byteInput = IOUtils.toByteArray(inStream);
                                result += datastreamsInput.datastreamsInput(new String(byteInput), schemaId,
                                        byteInput);
                            }
                            inStream.closeEntry();
                        }
                    } catch (IOException ex) {
                        result = "Error occured during fetch records from ZIP file.";
                    } finally {
                        if (inStream != null)
                            inStream.close();
                    }
                } else {
                    DatastreamsInput datastreamsInput = new DatastreamsInput();
                    datastreamsInput.setUploadedFileName(inputFileName);
                    result = datastreamsInput.datastreamsInput(new String(resultMap.get(inputFileName)),
                            schemaId, resultMap.get(inputFileName));
                }
            }
        }
    } else {
        result = "File Name not in specified format.";
    }

    // Store in Ftp location
    CamelContext context = exchange.getContext();
    FtpComponent component = context.getComponent("ftp", FtpComponent.class);
    FtpEndpoint<?> endpoint = (FtpEndpoint<?>) component.createEndpoint(getFTPEndPoint());

    Exchange outExchange = endpoint.createExchange();
    outExchange.getIn().setBody(result);
    outExchange.getIn().setHeader("CamelFileName", getFileNameWithoutExtensions(inputFileName) + ".txt");
    Producer producer = endpoint.createProducer();
    producer.start();
    producer.process(outExchange);
    producer.stop();
}

From source file:hd3gtv.embddb.network.DataBlock.java

/**
 * Import mode/*  www  .j a  v  a 2 s  . c  o m*/
 */
DataBlock(Protocol protocol, byte[] request_raw_datas) throws IOException {
    if (log.isTraceEnabled()) {
        log.trace("Get raw datas" + Hexview.LINESEPARATOR + Hexview.tracelog(request_raw_datas));
    }

    ByteArrayInputStream inputstream_client_request = new ByteArrayInputStream(request_raw_datas);

    DataInputStream dis = new DataInputStream(inputstream_client_request);

    byte[] app_socket_header_tag = new byte[Protocol.APP_SOCKET_HEADER_TAG.length];
    dis.readFully(app_socket_header_tag, 0, Protocol.APP_SOCKET_HEADER_TAG.length);

    if (Arrays.equals(Protocol.APP_SOCKET_HEADER_TAG, app_socket_header_tag) == false) {
        throw new IOException("Protocol error with app_socket_header_tag");
    }

    int version = dis.readInt();
    if (version != Protocol.VERSION) {
        throw new IOException(
                "Protocol error with version, this = " + Protocol.VERSION + " and dest = " + version);
    }

    byte tag = dis.readByte();
    if (tag != 0) {
        throw new IOException("Protocol error, can't found request_name raw datas");
    }

    int size = dis.readInt();
    if (size < 1) {
        throw new IOException(
                "Protocol error, can't found request_name raw datas size is too short (" + size + ")");
    }

    byte[] request_name_raw = new byte[size];
    dis.read(request_name_raw);
    request_name = new String(request_name_raw, Protocol.UTF8);

    tag = dis.readByte();
    if (tag != 1) {
        throw new IOException("Protocol error, can't found zip raw datas");
    }

    entries = new ArrayList<>(1);

    ZipInputStream request_zip = new ZipInputStream(dis);

    ZipEntry entry;
    while ((entry = request_zip.getNextEntry()) != null) {
        entries.add(new RequestEntry(entry, request_zip));
    }
    request_zip.close();
}

From source file:com.intuit.tank.standalone.agent.StandaloneAgentStartup.java

public void startTest(final StandaloneAgentRequest request) {
    Thread t = new Thread(new Runnable() {
        public void run() {
            try {
                currentAvailability.setAvailabilityStatus(AgentAvailabilityStatus.DELEGATING);
                sendAvailability();//from   w  ww  .j  av a2 s . com
                LOG.info("Starting up: ControllerBaseUrl=" + controllerBase);
                URL url = new URL(controllerBase + SERVICE_RELATIVE_PATH + METHOD_SETTINGS);
                LOG.info("Starting up: making call to tank service url to get settings.xml "
                        + url.toExternalForm());
                InputStream settingsStream = url.openStream();
                try {
                    String settings = IOUtils.toString(settingsStream);
                    FileUtils.writeStringToFile(new File("settings.xml"), settings);
                    LOG.info("got settings file...");
                } finally {
                    IOUtils.closeQuietly(settingsStream);
                }
                url = new URL(controllerBase + SERVICE_RELATIVE_PATH + METHOD_SUPPORT);
                LOG.info("Making call to tank service url to get support files " + url.toExternalForm());
                ZipInputStream zip = new ZipInputStream(url.openStream());
                try {
                    ZipEntry entry = zip.getNextEntry();
                    while (entry != null) {
                        String name = entry.getName();
                        LOG.info("Got file from controller: " + name);
                        File f = new File(name);
                        FileOutputStream fout = FileUtils.openOutputStream(f);
                        try {
                            IOUtils.copy(zip, fout);
                        } finally {
                            IOUtils.closeQuietly(fout);
                        }
                        entry = zip.getNextEntry();
                    }
                } finally {
                    IOUtils.closeQuietly(zip);
                }
                // now start the harness
                String cmd = API_HARNESS_COMMAND + " -http=" + controllerBase + " -jobId=" + request.getJobId()
                        + " -stopBehavior=" + request.getStopBehavior();
                LOG.info("Starting apiharness with command: " + cmd);
                currentAvailability.setAvailabilityStatus(AgentAvailabilityStatus.RUNNING_JOB);
                sendAvailability();
                Process exec = Runtime.getRuntime().exec(cmd);
                exec.waitFor();
                currentAvailability.setAvailabilityStatus(AgentAvailabilityStatus.AVAILABLE);
                sendAvailability();
                //
            } catch (Exception e) {
                LOG.error("Error in AgentStartup " + e, e);
                currentAvailability.setAvailabilityStatus(AgentAvailabilityStatus.AVAILABLE);
                sendAvailability();
            }
        }
    });
    t.start();
}

From source file:com.google.gdt.eclipse.designer.gwtext.actions.ConfigureGwtExtOperation.java

private static void extractZip(InputStream zipFile, IFolder targetFolder) throws Exception {
    ZipInputStream zipInputStream = new ZipInputStream(zipFile);
    while (true) {
        ZipEntry zipEntry = zipInputStream.getNextEntry();
        if (zipEntry == null) {
            break;
        }//from  ww w .j  a  va2 s  .  co  m
        if (!zipEntry.isDirectory()) {
            String entryName = zipEntry.getName();
            byte[] byteArray = IOUtils.toByteArray(zipInputStream);
            IOUtils2.setFileContents(targetFolder.getFile(new Path(entryName)),
                    new ByteArrayInputStream(byteArray));
        }
        zipInputStream.closeEntry();
    }
    zipInputStream.close();
}