Example usage for java.util.zip ZipInputStream getNextEntry

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

Introduction

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

Prototype

public ZipEntry getNextEntry() throws IOException 

Source Link

Document

Reads the next ZIP file entry and positions the stream at the beginning of the entry data.

Usage

From source file:lucee.commons.io.compress.CompressUtil.java

private static void listZip(Resource zipFile) throws IOException {
    if (!zipFile.exists())
        throw new IOException(zipFile + " is not a existing file");

    if (zipFile.isDirectory()) {
        throw new IOException(zipFile + " is a directory");
    }//from w  ww .j  av  a 2  s . c  o m

    ZipInputStream zis = null;
    try {
        zis = new ZipInputStream(IOUtil.toBufferedInputStream(zipFile.getInputStream()));
        ZipEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            if (!entry.isDirectory()) {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                IOUtil.copy(zis, baos, false, false);
                byte[] barr = baos.toByteArray();
                aprint.o(entry.getName() + ":" + barr.length);
            }
        }
    } finally {
        IOUtil.closeEL(zis);
    }
}

From source file:com.hazelcast.stabilizer.Utils.java

public static void unzip(byte[] content, final File destinationDir) throws IOException {
    byte[] buffer = new byte[1024];

    ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(content));
    ZipEntry zipEntry = zis.getNextEntry();

    while (zipEntry != null) {

        String fileName = zipEntry.getName();
        File file = new File(destinationDir + File.separator + fileName);

        //            log.finest("Unzipping: " + file.getAbsolutePath());

        if (zipEntry.isDirectory()) {
            file.mkdirs();//from  w  ww. j  a va 2  s.  co  m
        } else {
            file.getParentFile().mkdirs();

            FileOutputStream fos = new FileOutputStream(file);
            try {
                int len;
                while ((len = zis.read(buffer)) > 0) {
                    fos.write(buffer, 0, len);
                }
            } finally {
                closeQuietly(fos);
            }
        }

        zipEntry = zis.getNextEntry();
    }

    zis.closeEntry();
    zis.close();
}

From source file:lucee.commons.io.compress.CompressUtil.java

private static void unzip(Resource zipFile, Resource targetDir) throws IOException {
    /*if(zipFile instanceof File){
       unzip((File)zipFile, targetDir);// w  w w .  ja  v  a2  s  .com
       return;
    }*/

    ZipInputStream zis = null;
    try {
        zis = new ZipInputStream(IOUtil.toBufferedInputStream(zipFile.getInputStream()));
        ZipEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            Resource target = targetDir.getRealResource(entry.getName());
            if (entry.isDirectory()) {
                target.mkdirs();
            } else {
                Resource parent = target.getParentResource();
                if (!parent.exists())
                    parent.mkdirs();
                IOUtil.copy(zis, target, false);
            }
            target.setLastModified(entry.getTime());
            zis.closeEntry();
        }
    } finally {
        IOUtil.closeEL(zis);
    }
}

From source file:net.ftb.minecraft.MCInstaller.java

public static void launchMinecraft(String installDir, ModPack pack, LoginResponse resp, boolean isLegacy) {
    try {/*from   ww w  .j  a va  2  s.  c o m*/
        File packDir = new File(installDir, pack.getDir());
        String gameFolder = installDir + File.separator + pack.getDir() + File.separator + "minecraft";
        File gameDir = new File(packDir, "minecraft");
        File assetDir = new File(installDir, "assets");
        File libDir = new File(installDir, "libraries");
        File natDir = new File(packDir, "natives");
        final String packVer = Settings.getSettings().getPackVer(pack.getDir());

        Logger.logInfo("Setting up native libraries for " + pack.getName() + " v " + packVer + " MC "
                + pack.getMcVersion(packVer));
        if (!gameDir.exists())
            gameDir.mkdirs();

        if (natDir.exists()) {
            natDir.delete();
        }
        natDir.mkdirs();
        if (isLegacy)
            extractLegacy();
        Version base = JsonFactory.loadVersion(new File(installDir,
                "versions/{MC_VER}/{MC_VER}.json".replace("{MC_VER}", pack.getMcVersion(packVer))));
        byte[] buf = new byte[1024];
        for (Library lib : base.getLibraries()) {
            if (lib.natives != null) {
                File local = new File(libDir, lib.getPathNatives());
                ZipInputStream input = null;
                try {
                    input = new ZipInputStream(new FileInputStream(local));
                    ZipEntry entry = input.getNextEntry();
                    while (entry != null) {
                        String name = entry.getName();
                        int n;
                        if (lib.extract == null || !lib.extract.exclude(name)) {
                            File output = new File(natDir, name);
                            output.getParentFile().mkdirs();
                            FileOutputStream out = new FileOutputStream(output);
                            while ((n = input.read(buf, 0, 1024)) > -1) {
                                out.write(buf, 0, n);
                            }
                            out.close();
                        }
                        input.closeEntry();
                        entry = input.getNextEntry();
                    }
                } catch (Exception e) {
                    ErrorUtils.tossError("Error extracting native libraries");
                    Logger.logError("", e);
                } finally {
                    try {
                        input.close();
                    } catch (IOException e) {
                    }
                }
            }
        }
        List<File> classpath = new ArrayList<File>();
        Version packjson = new Version();
        if (!pack.getDir().equals("mojang_vanilla")) {
            if (isLegacy) {
                extractLegacyJson(new File(gameDir, "pack.json"));
            }
            if (new File(gameDir, "pack.json").exists()) {
                packjson = JsonFactory.loadVersion(new File(gameDir, "pack.json"));
                for (Library lib : packjson.getLibraries()) {
                    //Logger.logError(new File(libDir, lib.getPath()).getAbsolutePath());
                    classpath.add(new File(libDir, lib.getPath()));
                }
            }
        } else {
            packjson = base;
        }
        if (!isLegacy) //we copy the jar to a new location for legacy
            classpath.add(new File(installDir,
                    "versions/{MC_VER}/{MC_VER}.jar".replace("{MC_VER}", pack.getMcVersion(packVer))));
        else {
            FileUtils
                    .copyFile(
                            new File(installDir,
                                    "versions/{MC_VER}/{MC_VER}.jar".replace("{MC_VER}",
                                            pack.getMcVersion(packVer))),
                            new File(gameDir, "bin/" + Locations.OLDMCJARNAME));
            FileUtils.killMetaInf();
        }
        for (Library lib : base.getLibraries()) {
            classpath.add(new File(libDir, lib.getPath()));
        }

        Process minecraftProcess = MCLauncher.launchMinecraft(Settings.getSettings().getJavaPath(), gameFolder,
                assetDir, natDir, classpath, packjson.mainClass != null ? packjson.mainClass : base.mainClass,
                packjson.minecraftArguments != null ? packjson.minecraftArguments : base.minecraftArguments,
                packjson.assets != null ? packjson.assets : base.getAssets(),
                Settings.getSettings().getRamMax(), pack.getMaxPermSize(), pack.getMcVersion(packVer),
                resp.getAuth(), isLegacy);
        LaunchFrame.MCRunning = true;
        if (LaunchFrame.con != null)
            LaunchFrame.con.minecraftStarted();
        StreamLogger.prepare(minecraftProcess.getInputStream(), new LogEntry().level(LogLevel.UNKNOWN));
        String[] ignore = { "Session ID is token" };
        StreamLogger.setIgnore(ignore);
        StreamLogger.doStart();
        TrackerUtils.sendPageView(ModPack.getSelectedPack().getName() + " Launched",
                ModPack.getSelectedPack().getName());
        try {
            Thread.sleep(1500);
        } catch (InterruptedException e) {
        }
        try {
            minecraftProcess.exitValue();
        } catch (IllegalThreadStateException e) {
            LaunchFrame.getInstance().setVisible(false);
            LaunchFrame.setProcMonitor(ProcessMonitor.create(minecraftProcess, new Runnable() {
                @Override
                public void run() {
                    if (!Settings.getSettings().getKeepLauncherOpen()) {
                        System.exit(0);
                    } else {
                        if (LaunchFrame.con != null)
                            LaunchFrame.con.minecraftStopped();
                        LaunchFrame launchFrame = LaunchFrame.getInstance();
                        launchFrame.setVisible(true);
                        LaunchFrame.getInstance().getEventBus().post(new EnableObjectsEvent());
                        try {
                            Settings.getSettings()
                                    .load(new FileInputStream(Settings.getSettings().getConfigFile()));
                            LaunchFrame.getInstance().tabbedPane.remove(1);
                            LaunchFrame.getInstance().optionsPane = new OptionsPane(Settings.getSettings());
                            LaunchFrame.getInstance().tabbedPane.add(LaunchFrame.getInstance().optionsPane, 1);
                            LaunchFrame.getInstance().tabbedPane.setIconAt(1, LauncherStyle.getCurrentStyle()
                                    .filterHeaderIcon(this.getClass().getResource("/image/tabs/options.png")));
                        } catch (Exception e1) {
                            Logger.logError("Failed to reload settings after launcher closed", e1);
                        }
                    }
                    LaunchFrame.MCRunning = false;
                }
            }));
        }
    } catch (Exception e) {
        Logger.logError("Error while running launchMinecraft()", e);
    }
}

From source file:edu.harvard.i2b2.eclipse.plugins.metadataLoader.util.FileUtil.java

public static void unzip(String zipname) throws IOException {

    FileInputStream fis = new FileInputStream(zipname);
    ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
    //??    GZIPInputStream gzis = new GZIPInputStream(new BufferedInputStream(fis));

    // get directory of the zip file
    if (zipname.contains("\\"))
        zipname = zipname.replace("\\", "/");
    //   String zipDirectory = zipname.substring(0, zipname.lastIndexOf("/")+1) ;
    String zipDirectory = zipname.substring(0, zipname.lastIndexOf("."));
    new File(zipDirectory).mkdir();

    RunData.getInstance().setMetadataDirectory(zipDirectory);

    ZipEntry entry;/*from  w w  w. j  a va  2s. c o  m*/
    while ((entry = zis.getNextEntry()) != null) {
        System.out.println("Unzipping: " + entry.getName());
        if (entry.getName().contains("metadata")) {
            RunData.getInstance().setMetadataFile(zipDirectory + "/" + entry.getName());
        } else if (entry.getName().contains("schemes")) {
            RunData.getInstance().setSchemesFile(zipDirectory + "/" + entry.getName());
        } else if (entry.getName().contains("access")) {
            RunData.getInstance().setTableAccessFile(zipDirectory + "/" + entry.getName());
        }

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

        FileOutputStream fos = new FileOutputStream(zipDirectory + "/" + entry.getName());
        BufferedOutputStream bos = new BufferedOutputStream(fos, buffer.length);

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

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

/**
 * Checks whether the file referred by the given URL is an OOXML document.
 * /*  w ww  . ja v a2  s . c  om*/
 * @param url
 * @return
 * @throws IOException
 */
public static boolean isOOXML(URL url) throws IOException {
    ZipInputStream zipInputStream = new ZipInputStream(url.openStream());
    ZipEntry zipEntry;
    while (null != (zipEntry = zipInputStream.getNextEntry())) {
        if (false == "[Content_Types].xml".equals(zipEntry.getName())) {
            continue;
        }
        return true;
    }
    return false;
}

From source file:net.firejack.platform.core.utils.ArchiveUtils.java

public static void editzip(InputStream stream, OutputStream outputStream, boolean close,
        EditArchiveCallback callback, String... matches) {
    ZipInputStream in = new ZipInputStream(stream);
    ZipOutputStream zout = new ZipOutputStream(outputStream);
    ZipEntry entry;// w  w w.  j av  a 2  s  .com
    try {
        Map<String, File> skipped = new HashMap<String, File>();
        matches = (String[]) ArrayUtils.add(matches, ".*");
        while ((entry = in.getNextEntry()) != null) {
            String name = entry.getName();
            if (!entry.isDirectory()) {
                if (name.contains("/") && !File.separator.equals("/"))
                    name = name.replaceAll("/", File.separator + File.separator);
                if (name.contains("\\") && !File.separator.equals("\\"))
                    name = name.replaceAll("\\\\", File.separator);

                String dir = name.replaceAll("^(.*?)(?:\\\\|/?)([~@\\$\\{\\}\\w\\-\\.\\+]+$)", "$1");
                String file = name.replaceAll("^(.*?)(?:\\\\|/?)([~@\\$\\{\\}\\w\\-\\.\\+]+$)", "$2");

                if (file.matches(matches[0])) {
                    zout.putNextEntry(new ZipEntry(name));
                    callback.edit(dir, file, in, zout);
                    zout.closeEntry();
                } else {
                    File path = FileUtils.getTempFile(SecurityHelper.generateRandomSequence(16));
                    OutputStream out = FileUtils.openOutputStream(path);
                    IOUtils.copy(in, out);
                    IOUtils.closeQuietly(out);
                    skipped.put(name, path);
                }
            }
        }

        for (int i = 1; i < matches.length; i++) {
            for (Iterator<Map.Entry<String, File>> iterator = skipped.entrySet().iterator(); iterator
                    .hasNext();) {
                Map.Entry<String, File> next = iterator.next();
                String name = next.getKey();
                File path = next.getValue();

                String dir = name.replaceAll("(.*?)(?:\\\\|/?)([~@\\$\\{\\}\\w\\-\\.\\+]+$)", "$1");
                String file = name.replaceAll("(.*?)(?:\\\\|/?)([~@\\$\\{\\}\\w\\-\\.\\+]+$)", "$2");

                if (file.matches(matches[i])) {
                    iterator.remove();
                    FileInputStream inputStream = FileUtils.openInputStream(path);
                    zout.putNextEntry(entry);
                    callback.edit(dir, file, inputStream, zout);
                    zout.closeEntry();
                    IOUtils.closeQuietly(inputStream);
                    FileUtils.forceDelete(path);
                }
            }
        }

        while (callback.add(zout)) {
            zout.closeEntry();
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (close)
            IOUtils.closeQuietly(zout);
    }
}

From source file:cn.ipanel.apps.portalBackOffice.util.CommonsFiend.java

/**
 * marqueezip/*  w  w w  .  j  a  v  a 2 s. c o  m*/
 * @param imageList
 * @param zipfilePath
 * @return
 */
public static boolean validateMarqueeZipPic(List imageList, String zipfilePath) {
    if (imageList.size() == 0) {
        return true; //zip
    }
    if (FileFiend.judgeFileZip(zipfilePath) == false) { //zip
        return false;
    }
    boolean result = true;
    InputStream in = null;
    ZipInputStream zipInput = null;
    List zipPicList = new ArrayList();
    try {
        File file = new File(zipfilePath);
        in = new FileInputStream(file);
        zipInput = new ZipInputStream(in);
        ZipEntry zipEntry = null;
        while ((zipEntry = zipInput.getNextEntry()) != null) {
            String fileName = zipEntry.getName();
            if (CommonsFiend.validateImgFormat(fileName) == false) { //marqueeContent
                return false;
            }
            zipPicList.add(fileName);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (zipInput != null) {
                zipInput.closeEntry();
                zipInput.close();
            }
            if (in != null) {
                in.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    for (int i = 0; i < imageList.size(); i++) {
        String imageName = (String) imageList.get(i);
        boolean contain = false;
        for (int j = 0; j < zipPicList.size(); j++) {
            String fileName = (String) zipPicList.get(j);
            if (imageName.equals(fileName)) {
                contain = true; //
                break;
            }
        }
        if (contain == false) { //
            result = false;
            break;
        }
    }

    return result;
}

From source file:com.sldeditor.test.unit.tool.vector.VectorToolTest.java

/**
 * Unzip a zip file containing shp file.
 *
 * @param zipFilePath the zip file path//from  w  ww  . j av a2s  . c  o  m
 * @param destDirectory the dest directory
 * @throws IOException Signals that an I/O exception has occurred.
 */
private static void unzip(String zipFilePath, String destDirectory) throws IOException {
    File destDir = new File(destDirectory);
    if (!destDir.exists()) {
        destDir.mkdir();
    }
    ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
    ZipEntry entry = zipIn.getNextEntry();
    // iterates over entries in the zip file
    while (entry != null) {
        String filePath = destDirectory + File.separator + entry.getName();
        if (!entry.isDirectory()) {
            // if the entry is a file, extracts it
            extractFile(zipIn, filePath);
        } else {
            // if the entry is a directory, make the directory
            File dir = new File(filePath);
            dir.mkdir();
        }
        zipIn.closeEntry();
        entry = zipIn.getNextEntry();
    }
    zipIn.close();
}

From source file:com.taobao.android.tpatch.utils.JarSplitUtils.java

/**
 * ?jarentry/*from  w w w  .j a v a  2 s.  c o  m*/
 *
 * @param jarFile
 * @return
 */
public static List<String> getZipEntries(File jarFile) throws IOException {
    List<String> entries = new ArrayList<String>();
    FileInputStream fis = new FileInputStream(jarFile);
    ZipInputStream zis = new ZipInputStream(fis);
    try {
        // loop on the entries of the jar file package and put them in the final jar
        ZipEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            // do not take directories or anything inside a potential META-INF folder.
            if (entry.isDirectory()) {
                continue;
            }
            String name = entry.getName();
            entries.add(name);
        }
    } finally {
        zis.close();
    }
    return entries;
}