zip文件,带有c#MSDN错误 [英] zip file with c# MSDN error
本文介绍了zip文件,带有c#MSDN错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
using System;
using System.IO;
using System.IO.Compression;
namespace zip
{
public class Program
{
public static void Main()
{
// Path to directory of files to compress and decompress.
string dirpath = @"c:\users\public\reports";
DirectoryInfo di = new DirectoryInfo(dirpath);
// Compress the directory's files.
foreach (FileInfo fi in di.GetFiles())
{
Compress(fi);
}
// Decompress all *.gz files in the directory.
foreach (FileInfo fi in di.GetFiles("*.gz"))
{
Decompress(fi);
}
}
public static void Compress(FileInfo fi)
{
// Get the stream of the source file.
using (FileStream inFile = fi.OpenRead())
{
// Prevent compressing hidden and
// already compressed files.
if ((File.GetAttributes(fi.FullName)
& FileAttributes.Hidden)
!= FileAttributes.Hidden & fi.Extension != ".gz")
{
// Create the compressed file.
using (FileStream outFile =
File.Create(fi.FullName + ".gz"))
{
using (GZipStream Compress =
new GZipStream(outFile,
CompressionMode.Compress))
{
// Copy the source file into
// the compression stream.
inFile.CopyTo(Compress);
Console.WriteLine("Compressed {0} from {1} to {2} bytes.",
fi.Name, fi.Length.ToString(), outFile.Length.ToString());
}
}
}
}
}
public static void Decompress(FileInfo fi)
{
// Get the stream of the source file.
using (FileStream inFile = fi.OpenRead())
{
// Get original file extension, for example
// "doc" from report.doc.gz.
string curFile = fi.FullName;
string origName = curFile.Remove(curFile.Length -
fi.Extension.Length);
//Create the decompressed file.
using (FileStream outFile = File.Create(origName))
{
using (GZipStream Decompress = new GZipStream(inFile,
CompressionMode.Decompress))
{
// Copy the decompression stream
// into the output file.
Decompress.CopyTo(outFile);
Console.WriteLine("Decompressed: {0}", fi.Name);
}
}
}
}
}
}
我从视觉帮助中获得了此代码,但是当尝试在Decompress.CopyTo
上给我错误时
复制到
找不到此
为什么?
i get this code from visual help but when try give me error on Decompress.CopyTo
copyto
not find this
why?
推荐答案
我刚刚尝试了您的程序,并且看起来很吸引人.您是否有关于错误消息的更多详细信息?还请说明您尝试在哪个版本的.NET框架中运行该版本,以及您的VS版本.
我在VS 2010,OS:Vista Ultimate上使用C#.NET 4.0 Clientprofile进行了尝试.
问候,
Manfred
I just tried out your program and works like a charm. Do you have some more details on the error message? Please also state in what version of the .NET framework you tried to run this and also your VS version.
I tried it with C# .NET 4.0 Clientprofile on VS 2010, OS: Vista Ultimate.
Regards,
Manfred
太糟糕了,您没有正确报告错误.
这是一个.NET Framework版本(更确切地说是捆绑的GOC):该库没有v.4.0之前版本的CopyTo
方法.我尝试使用v.3.5进行编译-无法编译; 4.0确实可以.为了进行确认,当它与VS 2010一起编译时,我将目标更改为3.5 -不进行编译.我确定您可以实现缺少的方法.
Too bad you did not report the error properly.
This is a .NET Framework version (more exactly it''s GOC as bundled): the library does not haveCopyTo
methods for verions prior to v.4.0. I tried to compile with v.3.5 - does not compile; with 4.0 it does. For confirmation, when it compiles with VS 2010, I change the target to 3.5 -- it does not compile. I''m sure you could implement the missing methods.
这篇关于zip文件,带有c#MSDN错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文