Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class Main {

    public static void unCompress(String zipPath, String toPath) throws IOException {
        File zipfile = new File(zipPath);
        if (!zipfile.exists())
            return;

        if (!toPath.endsWith("/"))
            toPath += "/";

        File destFile = new File(toPath);
        if (!destFile.exists())
            destFile.mkdirs();

        ZipInputStream zis = new ZipInputStream(new FileInputStream(zipfile));
        ZipEntry entry = null;

        try {

            while ((entry = zis.getNextEntry()) != null) {
                if (entry.isDirectory()) {
                    File file = new File(toPath + entry.getName() + "/");
                    file.mkdirs();
                } else {
                    File file = new File(toPath + entry.getName());
                    if (!file.getParentFile().exists())
                        file.getParentFile().mkdirs();

                    FileOutputStream fos = null;

                    try {
                        fos = new FileOutputStream(file);
                        byte buf[] = new byte[1024];
                        int len = -1;
                        while ((len = zis.read(buf, 0, 1024)) != -1) {
                            fos.write(buf, 0, len);
                        }
                    } finally {

                        if (fos != null) {
                            try {
                                fos.close();
                            } catch (Exception e) {
                            }
                        }
                    }
                }
            }

        } finally {
            zis.close();
        }

    }
}