我想压缩文件,并在C#中的目录。我在互联网上找到了一些解决方案,但它们非常复杂,我无法在我的项目中运行它们。任何人都可以向我建议一个清晰有效的解决方案吗如何压缩文件
如何压缩文件
回答
非常感谢您提出的解决方案。我正在研究它。 – selentoptas
@aprogrammer不客气。我很抱歉没有发布一些代码,但我不知道我的确切位置,我现在没有太多时间 – CarlosJ
使用http://dotnetzip.codeplex.com/ zip文件或目录,也没有内建类直接在.NET做
谢谢@Arnaud F.我看到这个,但是我的程序不识别ZipFile。我如何在我的程序中描述它? – selentoptas
有在System.IO.Packaging
一个内置类称为ZipPackage
:
http://msdn.microsoft.com/en-us/library/system.io.packaging.zippackage(v=vs.100).aspx
在.NET 2.0中没有可用的用户请求 –
您可以在System.IO.Compression
名称空间中使用GZipStream
.NET 2.0.
public static void CompressFile(string path)
{
FileStream sourceFile = File.OpenRead(path);
FileStream destinationFile = File.Create(path + ".gz");
byte[] buffer = new byte[sourceFile.Length];
sourceFile.Read(buffer, 0, buffer.Length);
using (GZipStream output = new GZipStream(destinationFile,
CompressionMode.Compress))
{
Console.WriteLine("Compressing {0} to {1}.", sourceFile.Name,
destinationFile.Name, false);
output.Write(buffer, 0, buffer.Length);
}
// Close the files.
sourceFile.Close();
destinationFile.Close();
}
.NET 4
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());
}
}
}
}
}
感谢您的解决方案,但是我在这段代码中有一个关于“CopyTo”的错误。我无法解决这个问题。 – selentoptas
@Romil - 不,命名空间在2.0中可用,我检查过。您也可以在以下网址找到证明:http://msdn.microsoft.com/en-us/library/ms404280(v=vs.80).aspx –
@DarrenDavies,uow是正确的,它在2.0中引入。我读错了,我需要更多的咖啡。 –
Source code taken from MSDN that is compatible to .Net 2.0 and above
public static void CompressFile(string path)
{
FileStream sourceFile = File.OpenRead(path);
FileStream destinationFile = File.Create(path + ".gz");
byte[] buffer = new byte[sourceFile.Length];
sourceFile.Read(buffer, 0, buffer.Length);
using (GZipStream output = new GZipStream(destinationFile,
CompressionMode.Compress))
{
Console.WriteLine("Compressing {0} to {1}.", sourceFile.Name,
destinationFile.Name, false);
output.Write(buffer, 0, buffer.Length);
}
// Close the files.
sourceFile.Close();
destinationFile.Close();
}
我试图使用这个剪切,但我有一个关于到达文件路径的问题。我有一个关于它的错误?什么是不能到达文件路径的原因?您使用的是Windows 7 – selentoptas
。 –
是的,我使用的是Windows7。 – selentoptas
你可以使用MS-DOS命令在线程序compact.exe。 查找上的参数compact.exe在cmd并使用.NET方法的Process.Start(启动此过程)。
我加入这个答案,因为我发现比现有的任何答案更简单的方法:
- 在解决方案安装DotNetZip的DLL(最简单的方法就是安装the package从的NuGet)
- 添加对DLL的引用。
- 通过添加导入名称空间:使用Ionic.Zip;
- 邮编文件
代码:
using (ZipFile zip = new ZipFile())
{
zip.AddFile("C:\test\test.txt");
zip.AddFile("C:\test\test2.txt");
zip.Save("C:\output.zip");
}
如果你不想在zip文件反映了原来的文件夹结构,然后再看看的替换值AddFile()和AddFolder()等
使用DotNetZip http://dotnetzip.codeplex.com/,还有对zip文件类的AddDirectory()方法,你想要做什么:
using (var zip = new Ionic.Zip.ZipFile())
{
zip.AddDirectory("DirectoryOnDisk", "rootInZipFile");
zip.Save("MyFile.zip");
}
Bonne继续...
只需使用以下代码来压缩文件。
public void Compressfile()
{
string fileName = "Text.txt";
string sourcePath = @"C:\SMSDBBACKUP";
DirectoryInfo di = new DirectoryInfo(sourcePath);
foreach (FileInfo fi in di.GetFiles())
{
//for specific file
if (fi.ToString() == fileName)
{
Compress(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());
}
}
}
}
}
}
- 1. 如何压缩文件时不压缩
- 2. 如何将未压缩文件夹视为压缩文件?
- 3. 如何直接将csv文件压缩成zip压缩文件?
- 4. 如何压缩XML文件?
- 5. 如何压缩Excel文件?
- 6. 如何压缩文件
- 7. 如何解压/解压缩* .z文件
- 8. 如何解压/解压缩文件
- 9. NSIS:如何解压7zip压缩文件
- 10. 如何只压缩文件夹内的文件而不压缩子文件夹?
- 11. 压缩文件
- 12. 如何压缩文本文件
- 13. 如何压缩文本文件
- 14. 如何(高度)压缩文件并解压缩iOS
- 15. node.js如何流化22GB gzip压缩文件解压缩
- 16. 如何查找离子压缩文件的未压缩大小
- 17. 如何在内存中解压缩GZip压缩文件?
- 18. 如何启用HTTP压缩来压缩JSON文件
- 19. 如何解压缩已压缩两次的文件?
- 20. 如何生成文件,然后用Heroku压缩/压缩?
- 21. 如何使用AngularJs解压缩压缩文件?
- 22. 如何使用C#解压缩压缩文件?
- 23. 如何使用命令行解压缩xip压缩文件?
- 24. 如何使用Zend压缩过滤器压缩多个文件?
- 25. 如何解压缩使用fusecompress/lzo压缩的文件?
- 26. 如何使用rubyzip解压缩压缩文件夹
- 27. 如何在asp.net框架4.0中压缩/解压缩文件夹
- 28. 如何将“tail -f”压缩成压缩(gziped)文件?
- 29. 压缩文件未解压
- 30. Node.js - 压缩/解压缩文件夹
为什么不能在你的项目中运行它们? – BugFinder
是指在.NET 4.0 http://msdn.microsoft.com/en-us/library/ms404280.aspx –
可能重复[如何将目录压缩成zip文件编程(HTTP://计算器。com/questions/2498572 /如何压缩目录到一个zip文件编程) – ken2k