C# 如何压缩文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11153542/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to compress files
提问by selentoptas
I want to compress a file and a directory in C#. I found some solution in Internet but they are so complex and I couldn't run them in my project. Can anybody suggest me a clear and effective solution?
我想在 C# 中压缩一个文件和一个目录。我在 Internet 上找到了一些解决方案,但它们太复杂了,我无法在我的项目中运行它们。有人可以建议我一个明确有效的解决方案吗?
回答by CarlosJ
回答by Arnaud F.
Use http://dotnetzip.codeplex.com/to ZIP files or directory, there is no builtin class to do it directly in .NET
使用http://dotnetzip.codeplex.com/压缩文件或目录,.NET 中没有内置类可以直接完成
回答by Bas
There is a built-in class in System.IO.Packagingcalled the ZipPackage:
有一个内置类System.IO.Packaging叫做ZipPackage:
http://msdn.microsoft.com/en-us/library/system.io.packaging.zippackage(v=vs.100).aspx
http://msdn.microsoft.com/en-us/library/system.io.packaging.zippackage(v=vs.100).aspx
回答by Darren
You could use GZipStreamin the System.IO.Compressionnamespace
您可以GZipStream在System.IO.Compression命名空间中使用
.NET 2.0.
.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
.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());
}
}
}
}
}
回答by Romil Kumar Jain
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();
}
回答by ?ukasz Fija?kowski
You can just use ms-dos command line program compact.exe. Look on a parameters compact.exe in cmd and start this process using .NET method Process.Start().
您可以只使用 ms-dos 命令行程序 compact.exe。在 cmd 中查看参数 compact.exe 并使用 .NET 方法 Process.Start() 启动此进程。
回答by NickG
I'm adding this answer as I've found an easier way than any of the existing answers:
我添加这个答案是因为我找到了比任何现有答案更简单的方法:
- Install DotNetZipDLLs in your solution (easiest way is to install the packagefrom nuget)
- Add a reference to the DLL.
- Import the namespace by adding: using Ionic.Zip;
- Zip your file
Code:
代码:
using (ZipFile zip = new ZipFile())
{
zip.AddFile("C:\test\test.txt");
zip.AddFile("C:\test\test2.txt");
zip.Save("C:\output.zip");
}
If you don't want the original folder structure mirrored in the zip file, then look at the overrides for AddFile() and AddFolder() etc.
如果您不想在 zip 文件中镜像原始文件夹结构,请查看 AddFile() 和 AddFolder() 等的覆盖。
回答by Alexandre Jaslier
Using DotNetZip http://dotnetzip.codeplex.com/, there's an AddDirectory() method on the ZipFile class that does what you want:
使用 DotNetZip http://dotnetzip.codeplex.com/,在 ZipFile 类中有一个 AddDirectory() 方法可以执行您想要的操作:
using (var zip = new Ionic.Zip.ZipFile())
{
zip.AddDirectory("DirectoryOnDisk", "rootInZipFile");
zip.Save("MyFile.zip");
}
Bonne continuation...
博纳续...
回答by Jeetendra Negi
just use following code for compressing a file.
只需使用以下代码来压缩文件。
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());
}
}
}
}
}
}
回答by Waldo Sonntag
For .Net Framework 4.5 this is the most clear example I found:
对于 .Net Framework 4.5,这是我发现的最清楚的例子:
using System;
using System.IO;
using System.IO.Compression;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
string startPath = @"c:\example\start";
string zipPath = @"c:\example\result.zip";
string extractPath = @"c:\example\extract";
ZipFile.CreateFromDirectory(startPath, zipPath);
ZipFile.ExtractToDirectory(zipPath, extractPath);
}
}
}
You'll need to add a reference to System.IO.Compression.FileSystem
您需要添加对 System.IO.Compression.FileSystem 的引用
From: https://docs.microsoft.com/en-us/dotnet/standard/io/how-to-compress-and-extract-files
来自:https: //docs.microsoft.com/en-us/dotnet/standard/io/how-to-compress-and-extract-files

