给定以下代码,为什么解压缩不起作用? “ NewFile2.txt”应具有原始的解压缩文本,但文件仅为空白。
ioTests.CompressFile(@"c:\newfile.txt", @"c:\newfile.txt.gz");
ioTests.DecompressFile(@"c:\newfile.txt.gz", @"c:\newfile2.txt");
public void CompressFile(string inFileName, string outFileName)
{
FileStream inFile = new FileStream(inFileName, FileMode.Open);
FileStream outFile = new FileStream(outFileName, FileMode.Create);
GZipStream compStream = new GZipStream(outFile, CompressionMode.Compress);
int theByte = inFile.ReadByte();
while (theByte != -1)
{
compStream.WriteByte((byte)theByte);
theByte = inFile.ReadByte();
}
compStream.Close();
}
public void DecompressFile(string inFileName, string outFileName)
{
FileStream inFile = new FileStream(inFileName, FileMode.Open);
FileStream outFile = new FileStream(outFileName, FileMode.CreateNew);
GZipStream compStream = new GZipStream(inFile, CompressionMode.Decompress);
int theByte = compStream.ReadByte();
while (theByte != -1)
{
outFile.WriteByte((byte)theByte);
theByte = compStream.ReadByte();
}
compStream.Close();
}
最佳答案
outFile.Flush(); // after your loop