Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用java中的GZIP压缩文件_Java_Text_Binary_Format_Gzip - Fatal编程技术网

用java中的GZIP压缩文件

用java中的GZIP压缩文件,java,text,binary,format,gzip,Java,Text,Binary,Format,Gzip,我试着用缓冲输入流读取一个txt文件,然后用GZIP压缩它,它成功了。但是,当我试图用zip解压缩压缩文件时,文件似乎是无法读取的二进制格式,我该如何解决这个问题?以下是我的代码: public static void main(String[] args) { compressWithGZIP(SAVE_PATH2, SAVE_PATH3); //uncompressWithGZIP(SAVE_PATH3 + "compressed.gz", SAVE_PATH4); } p

我试着用缓冲输入流读取一个txt文件,然后用GZIP压缩它,它成功了。但是,当我试图用zip解压缩压缩文件时,文件似乎是无法读取的二进制格式,我该如何解决这个问题?以下是我的代码:

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();

        }
    }

}
经典错误

做。不曾经使用。读者。到阅读二元的。数据。

读取器
使用字符解码过程将从文件读取的数据解释为潜在字符。Java同时定义读取器vs InputStream和Writer vs OutputStream是有原因的

如果处理二进制数据,请使用InputStream和OutputStream。绝不使用读取器或写入器

换句话说,你的问题就在这里:

    br = new BufferedReader(new FileReader(filePath));
    bo = new BufferedOutputStream(new GZIPOutputStream(
            new FileOutputStream(outputPath)));
使用
输入流
,而不是
读取器
,读取源文件。

经典错误

做。不曾经使用。读者。到阅读二元的。数据。

读取器
使用字符解码过程将从文件读取的数据解释为潜在字符。Java同时定义读取器vs InputStream和Writer vs OutputStream是有原因的

如果处理二进制数据,请使用InputStream和OutputStream。绝不使用读取器或写入器

换句话说,你的问题就在这里:

    br = new BufferedReader(new FileReader(filePath));
    bo = new BufferedOutputStream(new GZIPOutputStream(
            new FileOutputStream(outputPath)));
使用
输入流
,而不是
读取器
,读取源文件