GZip解压给出空白文件
给出下面的代码,为什么解压不起作用? “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();
}
Given the code below, why is the decompression not working? "NewFile2.txt" should have the original, decompressed text, but the file is just blank.
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();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我更喜欢
这样刷新流并调用 Dispose 方法,释放分配的资源。
I prefer
as that flushes the stream and calls the Dispose method, releasing allocated resources.
由于您使用的流实现了
IDisposable
接口< /a>,您应该Dispose()
/Close()
您的类,或使用using
语句自动执行此操作:这大致翻译为到:
Since the streams you use implement the
IDisposable
interface, you shouldDispose()
/Close()
your classes, or use theusing
statement to do this automatically:This roughly translates to: