Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

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

import java.io.InputStream;
import java.util.zip.Adler32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

public class Main {

    public static boolean compress(File file) {
        try {
            String fileName = file.getName();
            if (fileName.indexOf(".") != -1)
                fileName = fileName.substring(0, fileName.indexOf("."));
            FileOutputStream f = new FileOutputStream(file.getParent() + "/" + fileName + ".zip");
            CheckedOutputStream cs = new CheckedOutputStream(f, new Adler32());
            ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(cs));
            InputStream in = new FileInputStream(file);
            out.putNextEntry(new ZipEntry(file.getName()));
            int len = -1;
            byte buf[] = new byte[1024];
            while ((len = in.read(buf, 0, 1024)) != -1)
                out.write(buf, 0, len);
            out.closeEntry();

            in.close();
            out.close();
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}