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.io.InputStream;

import java.io.OutputStream;

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

public class Main {
    public static void unpack(InputStream in, File targetDir) throws IOException {
        ZipInputStream zin = new ZipInputStream(in);
        ZipEntry entry;
        while ((entry = zin.getNextEntry()) != null) {
            String extractFilePath = entry.getName();
            if ((extractFilePath.startsWith("/")) || (extractFilePath.startsWith("\\"))) {
                extractFilePath = extractFilePath.substring(1);
            }
            File extractFile = new File(new StringBuilder().append(targetDir.getPath()).append(File.separator)
                    .append(extractFilePath).toString());
            if (entry.isDirectory()) {
                if (!extractFile.exists())
                    extractFile.mkdirs();
            } else {
                File parent = extractFile.getParentFile();
                if (!parent.exists()) {
                    parent.mkdirs();
                }

                FileOutputStream os = new FileOutputStream(extractFile);
                copyFile(zin, os);
                os.flush();
                os.close();
            }
        }
    }

    public static void copyFile(File in, File out) throws IOException {
        FileInputStream fis = new FileInputStream(in);
        if (!out.exists()) {
            if (in.isDirectory()) {
                out.mkdirs();
            } else {
                File parentDir = new File(out.getParent());
                if (!parentDir.exists()) {
                    parentDir.mkdirs();
                }
                out.createNewFile();
            }
        }
        FileOutputStream fos = new FileOutputStream(out);
        try {
            byte[] buf = new byte[8192];
            int i = 0;
            while ((i = fis.read(buf)) != -1)
                fos.write(buf, 0, i);
        } catch (IOException e) {
            throw e;
        } finally {
            fis.close();
            fos.close();
        }
    }

    public static void copyFile(InputStream in, OutputStream out) throws IOException {
        byte[] buffer = new byte[8192];
        int read;
        while ((read = in.read(buffer)) != -1) {
            out.write(buffer, 0, read);
        }
        out.flush();
    }

    public static byte[] read(File file) throws IOException {
        byte[] buffer = new byte[(int) file.length()];
        InputStream ios = null;
        try {
            ios = new FileInputStream(file);
            if (ios.read(buffer) == -1)
                throw new IOException("EOF reached while trying to read the whole file");
        } finally {
            try {
                if (ios != null)
                    ios.close();
            } catch (IOException e) {
            }
        }
        return buffer;
    }
}