在C#中使用Gzipstream类解压缩.gz文件 [英] Unzip .gz file using Gzipstream class in C#
本文介绍了在C#中使用Gzipstream类解压缩.gz文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请一些人请帮助我。我必须解压缩.gz文件。 ziped文件为33 MB,压缩后的文件大约为3GB。我写了下面的代码,能够解压缩只有256 kb的文件。
Hi ,
Couls some one please help me . I have to unzip a .gz file. The ziped file is of 33 MB and after zip it will be around 3GB . I have written the below code and able to unzip the file of only 256 kb.
public static void Main()
{
string path = @"\\lousqlwis18\Ftproot\CGXRPT2\Predictive_Score";
DirectoryInfo inf = new DirectoryInfo(path);
foreach (FileInfo f in inf.GetFiles())
{
if (f.Name.ToString() == "TEST.dat.gz")
{
decomp(f);
}
}
}
public static void decomp(FileInfo fi)
{
long i=0;long c;
using (FileStream inFile = fi.OpenRead())
{
string curFile = fi.FullName;
string origName = curFile.Remove(curFile.Length - fi.Extension.Length);
using (FileStream outFile = File.Create(origName))
{
using (GZipStream Dc = new GZipStream(inFile,
CompressionMode.Decompress))
{
byte[] buffer = new byte[10240];
int numRead;
while ((numRead = Dc.Read(buffer, 0, buffer.Length)) != 0)
{
outFile.Write(buffer, 0, numRead);
Console.Write(outFile.Length.ToString());
Console.Write(numRead.ToString());
c=(numRead+i);
Console.WriteLine(c.ToString());
}
Console.WriteLine("Decompressed: {0}", fi.Name);
}
}
}
}
谢谢,
Preetpal
Thanks,
Preetpal
推荐答案
它只是我吗?或者你是否比你需要的内环更难
我想的是像
is it just me ? or are you making the inner loop much harder than you need
I was thinking of something like
using (GZipStream Dc = new GZipStream(inFile, CompressionMode.Decompress))
{
Dc.CopyTo(outFile);
}
来自http://blogs.msdn.com/b/bclteam/archive/2005/06/15/429542.aspx [ ^ ]
from http://blogs.msdn.com/b/bclteam/archive/2005/06/15/429542.aspx[^]
private static void Compress(Stream source, Stream destination)
{
// We must explicitly close the output stream, or GZipStream will not
// write the compression's footer to the file. So we'll get a file, but
// we won't be able to decompress it. We'll get back 0 bytes.
using(GZipStream output = new GZipStream(destination, CompressionMode.Compress)) {
Pump(source, output);
}
}
private static void Decompress(Stream source, Stream destination)
{
using(GZipStream input = new GZipStream(source, CompressionMode.Decompress)) {
Pump(input, destination);
}
}
private static void Pump(Stream input, Stream output)
{
byte[] bytes = new byte[4096];
int n;
while((n = input.Read(bytes, 0, bytes.Length)) != 0) {
output.Write(bytes, 0, n);
}
}
...
...
String inputFileName = @"fully qualified path to input file";
String outputFileName = @"fully qualified path to output file";
// no checks/tidyups done on itput/output file(s)
Stream src = null;
Stream dest = null;
dest = File.Create(outputFileName);
using(src = File.OpenRead(inputFleName)) {
using(dest)
{
Decompress(src, dest);
}
}
这篇关于在C#中使用Gzipstream类解压缩.gz文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文