Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;

import java.io.FileOutputStream;

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

public class Main {
    private static final int buffer = 2048;

    public static void unZip(String path) {
        int count = -1;
        int index = -1;

        String savepath = "";

        savepath = path.substring(0, path.lastIndexOf("."));
        try {
            BufferedOutputStream bos = null;
            ZipEntry entry = null;
            FileInputStream fis = new FileInputStream(path);
            ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));

            while ((entry = zis.getNextEntry()) != null) {
                byte data[] = new byte[buffer];

                String temp = entry.getName();
                index = temp.lastIndexOf("/");
                if (index > -1)
                    temp = temp.substring(index + 1);
                String tempDir = savepath + "/zip/";
                File dir = new File(tempDir);
                dir.mkdirs();
                temp = tempDir + temp;
                File f = new File(temp);
                f.createNewFile();

                FileOutputStream fos = new FileOutputStream(f);
                bos = new BufferedOutputStream(fos, buffer);

                while ((count = zis.read(data, 0, buffer)) != -1) {
                    bos.write(data, 0, count);
                }

                bos.flush();
                bos.close();
            }

            zis.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}