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.util.zip.Deflater;

import java.util.zip.GZIPOutputStream;

import android.os.Environment;

public class Main {
    public static String compressGzipFile(String filename) {
        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() + "/" + "gzip.zip";

            try {
                FileInputStream fis = new FileInputStream(uncompressedFile);
                FileOutputStream fos = new FileOutputStream(compressedFile);
                GZIPOutputStream gzipOS = new GZIPOutputStream(fos) {
                    {
                        def.setLevel(Deflater.BEST_COMPRESSION);
                    }
                };

                byte[] buffer = new byte[1024];
                int len;
                while ((len = fis.read(buffer)) != -1) {
                    gzipOS.write(buffer, 0, len);
                }
                //close resources
                gzipOS.close();
                fos.close();
                fis.close();
                return compressedFile;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;

    }
}