Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * Copyright (c) 2002-2006 The European Bioinformatics Institute, and others.
 * All rights reserved. Please see the file LICENSE
 * in the root directory of this distribution.
 */

import java.io.*;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.*;

public class Main {
    /**
     * Uncompresses zipped files
     * @param zippedFile The file to uncompress
     * @return list of unzipped files
     * @throws java.io.IOException thrown if there is a problem finding or writing the files
     */
    public static List<File> unzip(File zippedFile) throws IOException {
        return unzip(zippedFile, null);
    }

    /**
     * Uncompresses zipped files
     * @param zippedFile The file to uncompress
     * @param destinationDir Where to put the files
     * @return  list of unzipped files
     * @throws java.io.IOException thrown if there is a problem finding or writing the files
     */
    public static List<File> unzip(File zippedFile, File destinationDir) throws IOException {
        int buffer = 2048;

        List<File> unzippedFiles = new ArrayList<File>();

        BufferedOutputStream dest;
        BufferedInputStream is;
        ZipEntry entry;
        ZipFile zipfile = new ZipFile(zippedFile);
        Enumeration e = zipfile.entries();
        while (e.hasMoreElements()) {
            entry = (ZipEntry) e.nextElement();

            is = new BufferedInputStream(zipfile.getInputStream(entry));
            int count;
            byte data[] = new byte[buffer];

            File destFile;

            if (destinationDir != null) {
                destFile = new File(destinationDir, entry.getName());
            } else {
                destFile = new File(entry.getName());
            }

            FileOutputStream fos = new FileOutputStream(destFile);
            dest = new BufferedOutputStream(fos, buffer);
            try {
                while ((count = is.read(data, 0, buffer)) != -1) {
                    dest.write(data, 0, count);
                }

                unzippedFiles.add(destFile);
            } finally {
                dest.flush();
                dest.close();
                is.close();
            }
        }

        return unzippedFiles;
    }
}