vb.net 压缩和解压缩文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12016217/
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
Zip and unzip files
提问by Darryl Janecek
I am a programmer using VS2012. I am wanting to unzip a zip file (made with Winzip, filzip or other zip compression routines) and then also be able to zip the files back up into a zip file.
我是一名使用 VS2012 的程序员。我想解压缩一个 zip 文件(使用 Winzip、filzip 或其他 zip 压缩例程制作),然后还能够将这些文件压缩回一个 zip 文件。
What is the best library to use for this and can I please have some sample code on how to use the library?
用于此目的的最佳库是什么,我能否提供一些有关如何使用该库的示例代码?
EDIT
编辑
I am using VB.net, here is my code:
我正在使用 VB.net,这是我的代码:
Public Function extractZipArchive() As Boolean
Dim zipPath As String = "c:\example\start.zip"
Dim extractPath As String = "c:\example\extract"
Using archive As ZipArchive = ZipFile.OpenRead(zipPath)
For Each entry As ZipArchiveEntry In archive.Entries
If entry.FullName.EndsWith(".txt", StringComparison.OrdinalIgnoreCase) Then
entry.ExtractToFile(Path.Combine(extractPath, entry.FullName))
End If
Next
End Using
End Function
What import statements do I need to use? Currently I have added the following:
我需要使用哪些导入语句?目前我已经添加了以下内容:
Imports System.IO
Imports System.IO.Compression
I am getting the error:
我收到错误:
Type 'ZipArchive' is not defined
未定义类型“ZipArchive”
How can I fix this error?
我该如何解决这个错误?
采纳答案by ta.speot.is
If you're using Visual Studio 2012 and the .NET Framework 4.5 you can use the new compression library:
如果您使用的是 Visual Studio 2012 和 .NET Framework 4.5 ,则可以使用新的压缩库:
//This stores the path where the file should be unzipped to,
//including any subfolders that the file was originally in.
string fileUnzipFullPath;
//This is the full name of the destination file including
//the path
string fileUnzipFullName;
//Opens the zip file up to be read
using (ZipArchive archive = ZipFile.OpenRead(zipName))
{
//Loops through each file in the zip file
foreach (ZipArchiveEntry file in archive.Entries)
{
//Outputs relevant file information to the console
Console.WriteLine("File Name: {0}", file.Name);
Console.WriteLine("File Size: {0} bytes", file.Length);
Console.WriteLine("Compression Ratio: {0}", ((double)file.CompressedLength / file.Length).ToString("0.0%"));
//Identifies the destination file name and path
fileUnzipFullName = Path.Combine(dirToUnzipTo, file.FullName);
//Extracts the files to the output folder in a safer manner
if (!System.IO.File.Exists(fileUnzipFullName))
{
//Calculates what the new full path for the unzipped file should be
fileUnzipFullPath = Path.GetDirectoryName(fileUnzipFullName);
//Creates the directory (if it doesn't exist) for the new path
Directory.CreateDirectory(fileUnzipFullPath);
//Extracts the file to (potentially new) path
file.ExtractToFile(fileUnzipFullName);
}
}
}
回答by Compassionate Narcissist
Unanswered, although a while ago, so I'll still put my $0.02 in there for anyone else who hits this on keywords...
没有得到答复,虽然不久前,所以我仍然会将我的 0.02 美元放在那里,供其他任何在关键字上点击此内容的人使用...
VB 2012 (.Net 4.5) added new features to System.IO.Compression (System.IO.Compression.FileSystem.dll) that will do what you want. We only had GZip before. You can still use the free DotNetZipor SharpZipLib, of course.
VB 2012 (.Net 4.5) 向 System.IO.Compression ( System.IO.Compression.FileSystem.dll) 添加了新功能,可以满足您的需求。我们之前只有 GZip。当然,您仍然可以使用免费的DotNetZip或SharpZipLib。
The ZipFileclass has 2 static methods that make simple compression/decompression drop-dead simple: CreateFromDirectoryand ExtractToDirectory. Yo also have compression choices of NoCompression, Fastest, and Optimal.
在使用ZipFile类有2种静态方法,使简单的压缩/解压缩下拉死亡简单:CreateFromDirectory和ExtractToDirectory。您还可以选择NoCompression、Fastest和Optimal压缩选项。
One thing about it that struck me about your post was the concept of files (even archives) within archives. With the ZipArchiveand ZipArchiveEntryclasses you can now
关于您的帖子让我印象深刻的一件事是档案中的文件(甚至档案)的概念。使用ZipArchive和ZipArchiveEntry类,您现在可以
ZipArchive:
压缩档案:
Using zippedFile as ZipArchive = ZipFile.Open("foo.zip", ZipArchiveMode.Read)
For Each ntry as ZipArchiveEntry In zippedFile.Entries
Debug.Writeline("entry " & ntry.FullName & " is only " & ntry.CompressedLength.ToString)
Next
End Using
Your question also was about adding to an existing archive. You can now do that like this:
您的问题还与添加到现有存档有关。你现在可以这样做:
Using zippedFile as ZipArchive = ZipFile.Open("foo.zip", ZipArchiveMode.Update)
zippedFile.createEntry("bar.txt", CompressionLevel.Fastest)
' likewise you can get an entry already in there...
Dim ntry As ZipArchiveEntry = zippedFile.GetEntry("wtf.doc")
' even delete an entry without need to decompress & compress again!
ntry.Delete() ' !
End Using
Again, this was a while ago, but a lot of us still use 2012, and as this change won't be going anywhere in future versions, it should still prove helpful moving forward if anyone hits in on a keyword/tag search...
同样,这是前一段时间,但我们很多人仍在使用 2012,并且由于此更改在未来版本中不会发生任何变化,因此如果有人点击关键字/标签搜索,它仍然会证明对前进有帮助。 .
...and we didn't even talkabout UTF-8 support!
...我们甚至没有谈论UTF-8 支持!
回答by Stephen D.
You probably aren't referencing System.IO.Compression
. Check the box for that assembly reference and it should eliminate the error.
你可能没有引用System.IO.Compression
. 选中该装配参考的框,它应该消除错误。
回答by Samuel Surya
As mentioned in https://msdn.microsoft.com/en-us/library/system.io.compression.zipfile(v=vs.110).aspx
如https://msdn.microsoft.com/en-us/library/system.io.compression.zipfile(v=vs.110).aspx 中所述
You can use ZipFile.ExtractToDirectoryand CreateFromDirectory
您可以使用ZipFile.ExtractToDirectory和CreateFromDirectory
This is the example:
这是示例:
Imports System.IO
Imports System.IO.Compression
Module Module1
Sub Main()
Dim startPath As String = "c:\example\start" Dim zipPath As String = "c:\example\result.zip" Dim extractPath As String = "c:\example\extract" ZipFile.CreateFromDirectory(startPath, zipPath) ZipFile.ExtractToDirectory(zipPath, extractPath)
End Sub
End Module
进口系统.IO
导入 System.IO.Compression
模块模块1
子主()
Dim startPath As String = "c:\example\start" Dim zipPath As String = "c:\example\result.zip" Dim extractPath As String = "c:\example\extract" ZipFile.CreateFromDirectory(startPath, zipPath) ZipFile.ExtractToDirectory(zipPath, extractPath)
结束子
终端模块
Make sure you have referenced System.IO.Compression.FileSystemfor using this function.
确保您已引用System.IO.Compression.FileSystem以使用此函数。
回答by ismailakarim
Dim fileStream As Stream = File.OpenRead("your file path")
Using zipToOpen As FileStream = New FileStream(".......\My.zip", FileMode.CreateNew)
Using archive As ZipArchive = New ZipArchive(zipToOpen, ZipArchiveMode.Create)
Dim readmeEntry As ZipArchiveEntry = archive.CreateEntry("your file path")
fileStream.CopyTo(readmeEntry.Open())
End Using
End Using