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

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

import android.os.Environment;

public class Main {
    public static String zip(String filename) throws IOException {
        BufferedInputStream origin = null;
        Integer BUFFER_SIZE = 20480;

        if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {

            File flockedFilesFolder = new File(
                    Environment.getExternalStorageDirectory() + File.separator + "FlockLoad");
            System.out.println("FlockedFileDir: " + flockedFilesFolder);
            String uncompressedFile = flockedFilesFolder.toString() + "/" + filename;
            String compressedFile = flockedFilesFolder.toString() + "/" + "flockZip.zip";
            ZipOutputStream out = new ZipOutputStream(
                    new BufferedOutputStream(new FileOutputStream(compressedFile)));
            out.setLevel(9);
            try {
                byte data[] = new byte[BUFFER_SIZE];
                FileInputStream fi = new FileInputStream(uncompressedFile);
                System.out.println("Filename: " + uncompressedFile);
                System.out.println("Zipfile: " + compressedFile);
                origin = new BufferedInputStream(fi, BUFFER_SIZE);
                try {
                    ZipEntry entry = new ZipEntry(
                            uncompressedFile.substring(uncompressedFile.lastIndexOf("/") + 1));
                    out.putNextEntry(entry);
                    int count;
                    while ((count = origin.read(data, 0, BUFFER_SIZE)) != -1) {
                        out.write(data, 0, count);
                    }
                } finally {
                    origin.close();
                }
            } finally {
                out.close();
            }
            return "flockZip.zip";
        }
        return null;
    }
}