Example usage for java.util.zip ZipInputStream read

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

Introduction

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

Prototype

public int read(byte[] b, int off, int len) throws IOException 

Source Link

Document

Reads from the current ZIP entry into an array of bytes.

Usage

From source file:org.apache.axis2.deployment.DeploymentEngine.java

private static void addAsWebResources(File in, String serviceFileName, AxisServiceGroup serviceGroup) {
    try {/*from ww w .j a  va  2 s .co m*/
        if (webLocationString == null) {
            return;
        }
        if (in.isDirectory()) {
            return;
        }
        File webLocation = new File(webLocationString);
        File out = new File(webLocation, serviceFileName);
        int BUFFER = 1024;
        byte data[] = new byte[BUFFER];
        FileInputStream fin = new FileInputStream(in);
        ZipInputStream zin = new ZipInputStream(fin);
        ZipEntry entry;
        while ((entry = zin.getNextEntry()) != null) {
            ZipEntry zip = new ZipEntry(entry);
            if (zip.getName().toUpperCase().startsWith("WWW")) {
                String fileName = zip.getName();
                fileName = fileName.substring("WWW/".length(), fileName.length());
                if (zip.isDirectory()) {
                    new File(out, fileName).mkdirs();
                } else {
                    FileOutputStream tempOut = new FileOutputStream(new File(out, fileName));
                    int count;
                    while ((count = zin.read(data, 0, BUFFER)) != -1) {
                        tempOut.write(data, 0, count);
                    }
                    tempOut.close();
                    tempOut.flush();
                }
                serviceGroup.setFoundWebResources(true);
            }
        }
        zin.close();
        fin.close();
    } catch (IOException e) {
        log.info(e.getMessage());
    }
}

From source file:net.sf.sveditor.core.tests.utils.BundleUtils.java

public void unpackBundleZipToFS(String bundle_path, File fs_path) {
    URL zip_url = fBundle.getEntry(bundle_path);
    TestCase.assertNotNull(zip_url);/* w  w w. ja  v  a 2  s.  co  m*/

    if (!fs_path.isDirectory()) {
        TestCase.assertTrue(fs_path.mkdirs());
    }

    try {
        InputStream in = zip_url.openStream();
        TestCase.assertNotNull(in);
        byte tmp[] = new byte[4 * 1024];
        int cnt;

        ZipInputStream zin = new ZipInputStream(in);
        ZipEntry ze;

        while ((ze = zin.getNextEntry()) != null) {
            // System.out.println("Entry: \"" + ze.getName() + "\"");
            File entry_f = new File(fs_path, ze.getName());
            if (ze.getName().endsWith("/")) {
                // Directory
                continue;
            }
            if (!entry_f.getParentFile().exists()) {
                TestCase.assertTrue(entry_f.getParentFile().mkdirs());
            }
            FileOutputStream fos = new FileOutputStream(entry_f);
            BufferedOutputStream bos = new BufferedOutputStream(fos, tmp.length);

            while ((cnt = zin.read(tmp, 0, tmp.length)) > 0) {
                bos.write(tmp, 0, cnt);
            }
            bos.flush();
            bos.close();
            fos.close();

            zin.closeEntry();
        }
        zin.close();
    } catch (IOException e) {
        e.printStackTrace();
        TestCase.fail("Failed to unpack zip file: " + e.getMessage());
    }
}

From source file:sloca.controller.FileUpload.java

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    try {//  w  w w  . j a v  a 2s .c o m

        //Create a factory for disk-based file items
        DiskFileItemFactory factory = new DiskFileItemFactory();

        //Configure a repository to ensure a secure temp location
        //is used
        ServletContext servletContext = this.getServletConfig().getServletContext();
        File repository = (File) servletContext.getAttribute("javax.servlet.context.tempdir");

        //Create a new file upload handler
        ServletFileUpload upload = new ServletFileUpload(factory);
        //Parse the reqeust
        List<FileItem> items = upload.parseRequest(request);
        //Process the uploaded items
        Iterator<FileItem> iter = items.iterator();
        while (iter.hasNext()) {
            FileItem item = iter.next();

            if (!item.isFormField()) {
                InputStream is = item.getInputStream();

                ZipInputStream zis = new ZipInputStream(new BufferedInputStream(is));
                ZipEntry ze;

                while ((ze = zis.getNextEntry()) != null) {
                    String filePath = repository + File.separator + ze.getName();
                    if (ze.getName().equals("demographics.csv") | ze.getName().equals("location-lookup.csv")
                            | ze.getName().equals("location.csv")) {
                        // out.println("entered print if<br/>");
                        int count;
                        byte data[] = new byte[BUFFER];
                        FileOutputStream fos = new FileOutputStream(filePath);
                        BufferedOutputStream bos = new BufferedOutputStream(fos, BUFFER);
                        while ((count = zis.read(data, 0, BUFFER)) != -1) {
                            bos.write(data, 0, count);
                        }
                        bos.flush();
                        bos.close();
                    }
                }
                zis.close();
            }
        }

        File[] files = new File(repository.toString()).listFiles();

        boolean isLocLookUpInserted = false;
        boolean isDemoInserted = false;
        boolean isLocInserted = false;

        //if uploaded folder does not contain the files, it will go back to bootstrap.jsp
        if (!isValidFile(files)) {

            request.setAttribute("fileUploadError", "Invalid .zip file");
            RequestDispatcher rd = request.getRequestDispatcher("adminDisplay.jsp");
            rd.forward(request, response);
            return;
        }
        //while all the files are not inserted

        while (!isLocLookUpInserted || !isDemoInserted || !isLocInserted) { // 
            for (File file : files) {
                String fileName = file.getName();
                String filePath = repository + File.separator + fileName;
                //out.println("</br>" + filePath);
                BufferedReader br = null;
                try {
                    br = new BufferedReader(new InputStreamReader(new FileInputStream(filePath)));
                    br.readLine();
                    String line = null;
                    if (!isDemoInserted && fileName.contains("demographics.csv")) {
                        BootStrapManager.processDemo(filePath);
                        isDemoInserted = true;

                    }
                    if (!isLocLookUpInserted && fileName.contains("location-lookup.csv")) {
                        BootStrapManager.processLocLookUp(filePath);
                        isLocLookUpInserted = true;
                    }
                    if (isDemoInserted && isLocLookUpInserted && !isLocInserted
                            && fileName.contains("location.csv")) {
                        BootStrapManager.processLoc(filePath);
                        isLocInserted = true;
                    }

                    br.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } finally {
                    if (br != null) {
                        br.close();
                    }
                }

                file.delete();
            }
        }

        response.sendRedirect("bootStrapResults.jsp");
    } catch (FileUploadException e) {
        out.println("gone case");
    } finally {
        out.close();

    }

}

From source file:com.arcusys.liferay.vaadinplugin.VaadinUpdater.java

private String exctractZipFile(File vaadinZipFile, String tmpPath) throws IOException {
    byte[] buf = new byte[1024];
    String zipDestinationPath = tmpPath + fileSeparator + "unzip" + fileSeparator;
    File unzipDirectory = new File(zipDestinationPath);
    if (!unzipDirectory.mkdir()) {
        log.warn("Zip extract failed.");
        upgradeListener.updateFailed("Zip extract failed: Can not create directory " + zipDestinationPath);
        return null;
    }//from w  ww. j  a va  2  s. c om

    ZipInputStream zinstream = new ZipInputStream(new FileInputStream(vaadinZipFile.getAbsolutePath()));
    ZipEntry zentry = zinstream.getNextEntry();

    while (zentry != null) {
        String entryName = zentry.getName();
        if (zentry.isDirectory()) {
            File newFile = new File(zipDestinationPath + entryName);
            if (!newFile.mkdir()) {
                break;
            }
            zentry = zinstream.getNextEntry();
            continue;
        }
        outputLog.log("Extracting " + entryName);
        FileOutputStream outstream = new FileOutputStream(
                zipDestinationPath + getFileNameWithoutVersion(entryName));
        int n;

        while ((n = zinstream.read(buf, 0, 1024)) > -1) {
            outstream.write(buf, 0, n);
        }

        outputLog.log("Successfully Extracted File Name : " + entryName);
        outstream.close();

        zinstream.closeEntry();
        zentry = zinstream.getNextEntry();
    }
    zinstream.close();

    return zipDestinationPath;
}

From source file:org.jdesktop.wonderland.artupload.ArtUploadServlet.java

/**
 * Write files to the art directory/*from ww w. j  ava 2s . c  om*/
 * @param items the list of items containing the files to write
 * @throws ServletException if there is an error writing the files
 */
protected void writeFiles(List<FileItem> items) throws IOException, ServletException {
    // get the value of the "name" field
    FileItem nameItem = findItem(items, "name");
    String name = nameItem.getString();

    // write the model file
    FileItem modelItem = findItem(items, "model");
    File modelsDir = new File(Util.getArtDir(getServletContext()), "models");
    File modelFile = new File(modelsDir, name + ".j3s.gz");

    try {
        modelItem.write(modelFile);
    } catch (Exception ex) {
        throw new ServletException(ex);
    }

    // unzip the textures
    FileItem texturesItem = findItem(items, "textures");
    ZipInputStream zin = new ZipInputStream(texturesItem.getInputStream());

    ZipEntry entry;
    File curDir = new File(Util.getArtDir(getServletContext()), "textures");
    while ((entry = zin.getNextEntry()) != null) {
        if (entry.isDirectory()) {
            File dir = new File(curDir, entry.getName());
            dir.mkdirs();
        } else {
            // write the unzipped texture data
            File texture = new File(curDir, entry.getName());
            FileOutputStream out = new FileOutputStream(texture);

            byte[] buffer;
            if (entry.getSize() > -1) {
                buffer = new byte[(int) entry.getSize()];
            } else {
                buffer = new byte[64 * 1024];
            }

            int read = 0;
            while ((read = zin.read(buffer, 0, buffer.length)) > -1) {
                out.write(buffer, 0, read);
            }
            out.close();
        }
    }
}

From source file:org.wso2.carbon.apacheds.impl.CarbonSchemaLdifExtractor.java

protected void unzipSchemaFile() throws IOException {
    try {/*from w  w w  .j  a v a 2s .  co m*/
        FileInputStream schemaFileStream = new FileInputStream(this.zipSchemaStore);
        ZipInputStream zipFileStream = new ZipInputStream(new BufferedInputStream(schemaFileStream));
        ZipEntry entry;

        String basePath = this.schemaDirectory.getAbsolutePath();

        while ((entry = zipFileStream.getNextEntry()) != null) {

            if (entry.isDirectory()) {
                File newDirectory = new File(basePath, entry.getName());
                if (!newDirectory.mkdir()) {
                    throw new IOException("Unable to create directory - " + newDirectory.getAbsolutePath());
                }
                continue;
            }

            int size;
            byte[] buffer = new byte[2048];

            FileOutputStream extractedSchemaFile = new FileOutputStream(new File(basePath, entry.getName()));
            BufferedOutputStream extractingBufferedStream = new BufferedOutputStream(extractedSchemaFile,
                    buffer.length);

            while ((size = zipFileStream.read(buffer, 0, buffer.length)) != -1) {
                extractingBufferedStream.write(buffer, 0, size);
            }
            extractingBufferedStream.flush();
            extractingBufferedStream.close();
        }

        zipFileStream.close();
        schemaFileStream.close();
    } catch (IOException e) {
        String msg = "Unable to extract schema directory to location " + this.schemaDirectory.getAbsolutePath()
                + " from " + this.zipSchemaStore.getAbsolutePath();
        logger.error(msg, e);
        throw new IOException(msg, e);
    }

    if (logger.isDebugEnabled()) {
        logger.debug("Successfully extracted schema files to path " + this.schemaDirectory.getAbsolutePath()
                + " using schema zip file " + this.zipSchemaStore.getAbsolutePath());
    }

}

From source file:org.candlepin.sync.ExporterTest.java

private void verifyContent(File export, String name, Verify v) {
    ZipInputStream zis = null;

    try {/*from  w ww. j a v  a2s. c  om*/
        zis = new ZipInputStream(new FileInputStream(export));
        ZipEntry entry = null;
        while ((entry = zis.getNextEntry()) != null) {
            byte[] buf = new byte[1024];

            if (entry.getName().equals("consumer_export.zip")) {
                OutputStream os = new FileOutputStream("/tmp/consumer_export.zip");

                int n;
                while ((n = zis.read(buf, 0, 1024)) > -1) {
                    os.write(buf, 0, n);
                }
                os.flush();
                os.close();
                File exportdata = new File("/tmp/consumer_export.zip");
                // open up the zip and look for the metadata
                verifyContent(exportdata, name, v);
            } else if (entry.getName().equals(name)) {
                v.verify(zis, buf);
            }
            zis.closeEntry();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (zis != null) {
            try {
                zis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:org.geon.XMLToADN.java

public static String unzip(java.io.File dst, java.io.File zip) throws Exception {

    System.out.println("dst folder = " + dst.getAbsolutePath() + " , zip file = " + zip.getAbsolutePath());
    int BUFFER = 2048;
    BufferedOutputStream dest = null;
    FileInputStream fis = new FileInputStream(zip);
    ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
    ZipEntry entry;/*from w  w w  . j a  v  a2 s .  c o m*/
    String name = null;
    boolean error = false;
    while ((entry = zis.getNextEntry()) != null) {

        if (entry.isDirectory()) {
            continue;
        }

        String tmp = entry.getName();
        String nm = tmp;

        int index = tmp.lastIndexOf("/");
        if (index != -1) {
            tmp = tmp.substring(index + 1);
            nm = tmp;
        }

        if (tmp.endsWith("dbf") || tmp.endsWith("shp") || tmp.endsWith("shx")) {

            tmp = tmp.substring(0, tmp.length() - 4);
            if (name == null) {
                name = tmp;
            } else if (name.equals(tmp)) {

            } else {
                continue;
            }
        }

        int count;
        byte data[] = new byte[BUFFER];

        // write the files to the disk
        java.io.File file = new java.io.File(dst, nm);
        FileOutputStream fos = new FileOutputStream(file);
        dest = new BufferedOutputStream(fos, BUFFER);
        while ((count = zis.read(data, 0, BUFFER)) != -1) {
            dest.write(data, 0, count);
        }
        dest.flush();
        dest.close();
    }
    zis.close();
    fis.close();

    return name;

}

From source file:net.pms.configuration.DownloadPlugins.java

private void unzip(File f, String dir) {
    // Zip file with loads of goodies
    // Unzip it//  w  ww .j  a va 2 s.  c  om
    ZipInputStream zis;
    try {
        zis = new ZipInputStream(new FileInputStream(f));
        ZipEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            File dst = new File(dir + File.separator + entry.getName());
            if (entry.isDirectory()) {
                dst.mkdirs();
                continue;
            }
            int count;
            byte data[] = new byte[4096];
            FileOutputStream fos = new FileOutputStream(dst);
            try (BufferedOutputStream dest = new BufferedOutputStream(fos, 4096)) {
                while ((count = zis.read(data, 0, 4096)) != -1) {
                    dest.write(data, 0, count);
                }
                dest.flush();
            }
            if (dst.getAbsolutePath().endsWith(".jar")) {
                jars.add(dst.toURI().toURL());
            }
        }
        zis.close();
    } catch (Exception e) {
        LOGGER.info("unzip error " + e);
    }
    f.delete();
}

From source file:link.kjr.file_manager.MainActivity.java

public void decompressZipFile(String path) throws IOException {
    FileInputStream fis = new FileInputStream(path);
    ZipInputStream zis = new ZipInputStream(fis);
    ZipEntry ze = zis.getNextEntry();
    while (ze != null) {

        File f = new File(currentPath + "/" + ze.getName());

        File parentfile = f.getParentFile();
        parentfile.mkdirs();//from ww  w  .j a v a  2 s  . com

        if (!f.exists()) {
            f.createNewFile();
        }
        FileOutputStream fos = new FileOutputStream(currentPath + "/" + ze.getName());
        byte[] data = new byte[1024];
        int length = 0;
        length = zis.read(data, 0, 1024);
        fos.write(data, 0, length);
        fos.close();
        zis.closeEntry();
        ze = zis.getNextEntry();
    }
    zis.close();
    fis.close();
}