使用 C# 压缩文件
我想使用 C#.Net 将一个“CSV”文件压缩到 Zip 文件中。下面我编写了一些用于创建 Zip 文件的代码,使用此代码我可以创建 zip 文件,但在创建“Data1.zip”文件后手动提取意味着提取的文件扩展名应该是“.csv”,但它不会出现。
FileStream sourceFile = File.OpenRead(@"C:\Users\Rav\Desktop\rData1.csv");
FileStream destFile = File.Create(@"C:\Users\Rav\Desktop\Data1.zip");
GZipStream compStream = new GZipStream(destFile, CompressionMode.Compress,false);
try
{
int theByte = sourceFile.ReadByte();
while (theByte != -1)
{
compStream.WriteByte((byte)theByte);
theByte = sourceFile.ReadByte();
}
}
finally
{
compStream.Dispose();
}
I want to zip one "CSV" file in to Zip file using C#.Net. Below i have written some code for create Zip file , using this code i am able to create zip file but after creating "Data1.zip" file extract manually means extracted file extension should be ".csv" but it is not coming.
FileStream sourceFile = File.OpenRead(@"C:\Users\Rav\Desktop\rData1.csv");
FileStream destFile = File.Create(@"C:\Users\Rav\Desktop\Data1.zip");
GZipStream compStream = new GZipStream(destFile, CompressionMode.Compress,false);
try
{
int theByte = sourceFile.ReadByte();
while (theByte != -1)
{
compStream.WriteByte((byte)theByte);
theByte = sourceFile.ReadByte();
}
}
finally
{
compStream.Dispose();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
http://msdn.microsoft.com/en- us/library/system.io.compression.gzipstream.aspx
这是 gzip 压缩,显然它只压缩一个流,解压缩时采用不带
.gz
的存档名称扩大。我不知道我是否在这里。你不妨尝试一下 MSDN 上的代码,看看是否有效。我使用 ZipLib 进行 zip 压缩。它还支持Bz2,这是一种很好的压缩算法。
http://msdn.microsoft.com/en-us/library/system.io.compression.gzipstream.aspx
This is gzip compression, and apparently it only compresses a stream, which when decompressed takes the name of the archive without the
.gz
extension. I don't know if I'm right here though. You might as well experiment with the code from MSDN, see if it works.I used ZipLib for zip compression. It also supports Bz2, which is a good compression algorithm.
使用 ICSharpCode.SharpZipLib(您可以下载它)并执行以下操作
希望它有帮助...
Use ICSharpCode.SharpZipLib(you can download it) and do the following
Hope it helps...
使用以下库之一:
http://www.icsharpcode.net/opensource/sharpziplib/
http://dotnetzip.codeplex.com/
我更喜欢#ziplib,但两者都有详细的文档记录并且广泛传播。
Use one of these libraries:
http://www.icsharpcode.net/opensource/sharpziplib/
http://dotnetzip.codeplex.com/
I prefer #ziplib, but both are well documented and widely spread.
从 .NET Framework 4.5 开始,您可以使用内置的 ZipFile 类(在 System.IO.Compression 命名空间中)。
Since .NET Framework 4.5, you can use the built-in ZipFile class (In the System.IO.Compression namespace).
在这里查看 FileSelectionManager 库:www.fileselectionmanager.com
首先,您必须将文件选择管理器 DLL 添加到您的项目
这是一个压缩的示例:
这是一个解压缩的示例:
希望它有所帮助。
Take a look at the FileSelectionManager library here: www.fileselectionmanager.com
First you have to add File Selection Manager DLL to your project
Here is an example for zipping:
Here is an example for unzipping:
Hope it helps.
我使用dll fileselectionmanager来压缩和解压缩文件和文件夹,它在我的项目中工作正常。您可以在您的网站中查看示例 http://www.fileselectionmanager.com/#Zipping 和解压文件
和文档 http://www.fileselectionmanager.com/file_selection_manager_documentation
I use the dll fileselectionmanager to compress and decompress files and folders, it has worked properly in my project. You can see example in your web http://www.fileselectionmanager.com/#Zipping and Unzipping files
and documentation http://www.fileselectionmanager.com/file_selection_manager_documentation