简体   繁体   中英

compress file with GZIP in java

I tried to read a txt file with the buffered input stream, and compress it with GZIP, it worked. However, when I try to extract the compressed file with the zip, the file seems in unreadable binary format, how do I solve this problem? The following is my code:

public static void main(String[] args) {
    compressWithGZIP(SAVE_PATH2, SAVE_PATH3);
    //uncompressWithGZIP(SAVE_PATH3 + "compressed.gz", SAVE_PATH4);
}

private static void uncompressWithGZIP(String oripath, String outputPath) {
    BufferedInputStream bi = null;
    BufferedOutputStream bo = null;
    try {
        bi = new BufferedInputStream(new GZIPInputStream(
                new FileInputStream(oripath)));
        bo = new BufferedOutputStream(new FileOutputStream(outputPath));
        int c;
        while ((c = bi.read()) != -1) {
            bo.write(c);
        }
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        try {
            if (bi != null) {
                bi.close();
            }
            if (bo != null) {
                bo.close();
            }
        } catch (Exception e) {
            e.printStackTrace();

        }
    }
}

private static void compressWithGZIP(String filePath, String outputPath) {
    if (outputPath == null || outputPath.isEmpty()
            || !outputPath.endsWith(".gz")) {
        outputPath += "compressed.gz";
    }

    BufferedReader br = null;
    BufferedOutputStream bo = null;
    try {
        br = new BufferedReader(new FileReader(filePath));
        bo = new BufferedOutputStream(new GZIPOutputStream(
                new FileOutputStream(outputPath)));
        int c;
        while ((c = br.read()) != -1) {
            bo.write(c);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (br != null) {
                br.close();
            }
            if (bo != null) {
                bo.close();
            }
        } catch (Exception e) {
            e.printStackTrace();

        }
    }

}

Classical mistake.

DO. NOT. EVER. USE. A. READER. TO. READ. BINARY. DATA. .

A Reader interprets data read from a file as potential characters using a character decoding process. There is a reason why Java defines both Reader vs InputStream and Writer vs OutputStream.

If you deal with binary data, use InputStream and OutputStream. NEVER Reader or Writer.

In other words your problem is here:

    br = new BufferedReader(new FileReader(filePath));
    bo = new BufferedOutputStream(new GZIPOutputStream(
            new FileOutputStream(outputPath)));

Use an InputStream , not a Reader , to read from the source file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM