DeflaterOutputStream不会输出有效文件

时间:2015-03-11 04:09:42

标签: java io

我将DeflaterOutputStream与Buffered输入和输出流结合使用,试图压缩一个简单的文件,我应该可以用第二个程序解压缩它(它还在一​​行的末尾添加一个数字但是这是无关紧要的)。但是,它没有创建有效的压缩文件。当我尝试解压缩它时,它只是创建一个空白文件。我认为它可能与潮红有关。有什么想法吗?

public class Program {

static String inputFileName = "inputfile.txt";
static String outputFileName = "outputfile.txt";

public static void main(String[] args) {    
    try {
        FileInputStream fileInputStream = new FileInputStream(inputFileName);
        BufferedInputStream inputBuff = new BufferedInputStream(fileInputStream);
        FileOutputStream fileOutputStream = new FileOutputStream(outputFileName);
        BufferedOutputStream outputBuff = new BufferedOutputStream(fileOutputStream);
        DeflaterOutputStream deflater = new DeflaterOutputStream(outputBuff);
        int fileByte;
        while ((fileByte =inputBuff.read()) != -1)
        {
            deflater.write(fileByte);
        }
        deflater.flush();
        outputBuff.flush();
        fileOutputStream.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
}

public class Program2 {

static String inputFileName = "outputfile.txt";
static String outputFileName = "decoutputfile.txt";

public static void main(String[] args) {    
    try {
        FileInputStream fileInputStream = new FileInputStream(inputFileName);
        BufferedInputStream inputBuff = new BufferedInputStream(fileInputStream);
        FileOutputStream fileOutputStream = new FileOutputStream(outputFileName);
        BufferedOutputStream outputBuff = new BufferedOutputStream(fileOutputStream);
        InflaterOutputStream inflater = new InflaterOutputStream(outputBuff);
        int fileByte;
        int lineCount = 1;
        while ((fileByte =inputBuff.read()) != -1)
        {
            if (fileByte == '\n'){
                inflater.write(lineCount);
                lineCount++;
            }
            inflater.write(fileByte);
        }
        inflater.flush();
        outputBuff.flush();
        fileOutputStream.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

}

0 个答案:

没有答案
相关问题