Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

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.ZipOutputStream;

public class Main {

    static String rmPath(String fName) {
        int pos = fName.lastIndexOf(File.separatorChar);
        if (pos > -1)
            fName = fName.substring(pos + 1);
        return fName;
    }

    public static void main(String args[]) throws Exception {
        ZipOutputStream fout = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(args[0])));

        for (int n = 1; n < args.length; n++) {

            BufferedInputStream fin = new BufferedInputStream(new FileInputStream(args[n]));
            ZipEntry ze = new ZipEntry(rmPath(args[n]));

            fout.putNextEntry(ze);

            int i;
            do {
                i = fin.read();
                if (i != -1)
                    fout.write(i);
            } while (i != -1);

            fout.closeEntry();
            fin.close();

            System.out.println("Compressing " + args[n]);
            System.out.println(
                    " Original Size: " + ze.getSize() + " Compressed Size: " + ze.getCompressedSize() + "\n");
        }
        fout.close();
    }
}