在C#中看起来没有内置的Library / API来解压缩zip文件。我正在寻找一个免费的(更好的开源)库/ API,它可以与.Net 3.5 + VSTS 2008 + C#一起解压zip文件并将所有文件解压缩到一个特定的文件夹中。
任何推荐的库/ API或样品?
答案 0 :(得分:34)
GPL
http://www.icsharpcode.net/OpenSource/SharpZipLib/
或限制较少的Ms-PL
http://www.codeplex.com/DotNetZip
要完成此答案,.net框架已ZipPackage我的成功率较低。
答案 1 :(得分:24)
如果您只想将文件内容解压缩到文件夹,并且您知道自己只能在Windows上运行,则可以使用Windows Shell对象。我在本例中使用了Framework 4.0中的dynamic
,但您可以使用Type.InvokeMember
获得相同的结果。
dynamic shellApplication = Activator.CreateInstance(Type.GetTypeFromProgID("Shell.Application"));
dynamic compressedFolderContents = shellApplication.NameSpace(sourceFile).Items;
dynamic destinationFolder = shellApplication.NameSpace(destinationPath);
destinationFolder.CopyHere(compressedFolderContents);
您可以使用FILEOP_FLAGS来控制CopyHere
方法的行为。
答案 2 :(得分:17)
DotNetZip易于使用。这是一个解压缩样本
using (var zip = Ionic.Zip.ZipFile.Read("archive.zip"))
{
zip.ExtractAll("unpack-directory");
}
如果您有更复杂的需求,例如您想要选择要提取的条目,或者是否有密码,或者您想要控制提取的文件的路径名等,等等,很多选择。 Check the help file了解更多示例。
DotNetZip是免费和开源的。
答案 3 :(得分:10)
过去,我使用过DotNetZip(MS-PL),SharpZipLib(GPL)和7ZIP SDK for C#(公共域)。它们都很好用,而且是开源的。
在这种情况下我会选择DotNetZip,这里有一些来自C# Examples page的示例代码:
using (ZipFile zip = ZipFile.Read(ExistingZipFile))
{
foreach (ZipEntry e in zip)
{
e.Extract(TargetDirectory);
}
}
答案 4 :(得分:7)
答案 5 :(得分:4)
查看我的小型图书馆:http://zipstorer.codeplex.com
答案 6 :(得分:2)
如果您想使用7-zip压缩,请查看Peter Bromberg的EggheadCafe article。注意:c#的LZMA source code没有xml注释(实际上,很少有注释)。
答案 7 :(得分:1)
如果你不想使用外部组件,这是我昨晚使用.NET的ZipPackage类开发的一些代码。
private static void Unzip()
{
var zipFilePath = "c:\\myfile.zip";
var tempFolderPath = "c:\\unzipped";
using (Package pkg = ZipPackage.Open(zipFilePath, FileMode.Open, FileAccess.Read))
{
foreach (PackagePart part in pkg.GetParts())
{
var target = Path.GetFullPath(Path.Combine(tempFolderPath, part.Uri.OriginalString.TrimStart('/')));
var targetDir = target.Remove(target.LastIndexOf('\\'));
if (!Directory.Exists(targetDir))
Directory.CreateDirectory(targetDir);
using (Stream source = part.GetStream(FileMode.Open, FileAccess.Read))
{
CopyStream(source, File.OpenWrite(target));
}
}
}
}
private static void CopyStream(Stream input, Stream output)
{
byte[] buffer = new byte[4096];
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
output.Write(buffer, 0, read);
}
}
注意事项:
ZIP存档必须在其根目录中有一个[Content_Types] .xml文件。这对我的要求来说不是问题,因为我将控制通过此代码提取的任何ZIP文件的压缩。有关[Content_Types] .xml文件的更多信息,请参阅:http://msdn.microsoft.com/en-us/magazine/cc163372.aspx本文的图13下面有一个示例文件。
我没有测试过CopyStream方法以确保它的行为正确,因为我最初使用Stream.CopyTo()方法为.NET 4.0开发了这个方法。
答案 8 :(得分:1)
#region CreateZipFile
public void StartZip(string directory, string zipfile_path)
{
Label1.Text = "Please wait, taking backup";
#region Taking files from root Folder
string[] filenames = Directory.GetFiles(directory);
// path which the zip file built in
ZipOutputStream p = new ZipOutputStream(File.Create(zipfile_path));
foreach (string filename in filenames)
{
FileStream fs = File.OpenRead(filename);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
ZipEntry entry = new ZipEntry(filename);
p.PutNextEntry(entry);
p.Write(buffer, 0 , buffer.Length);
fs.Close();
}
#endregion
string dirName= string.Empty;
#region Taking folders from root folder
DirectoryInfo[] DI = new DirectoryInfo(directory).GetDirectories("*.*", SearchOption.AllDirectories);
foreach (DirectoryInfo D1 in DI)
{
// the directory you need to zip
filenames = Directory.GetFiles(D1.FullName);
if (D1.ToString() == "backup")
{
filenames = null;
continue;
}
if (dirName == string.Empty)
{
if (D1.ToString() == "updates" || (D1.Parent).ToString() == "updates" || (D1.Parent).Parent.ToString() == "updates" || ((D1.Parent).Parent).Parent.ToString() == "updates" || (((D1.Parent).Parent).Parent).ToString() == "updates" || ((((D1.Parent).Parent).Parent)).ToString() == "updates")
{
dirName = D1.ToString();
filenames = null;
continue;
}
}
else
{
if (D1.ToString() == dirName) ;
}
foreach (string filename in filenames)
{
FileStream fs = File.OpenRead(filename);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
ZipEntry entry = new ZipEntry(filename);
p.PutNextEntry(entry);
p.Write(buffer, 0, buffer.Length);
fs.Close();
}
filenames = null;
}
p.SetLevel(5);
p.Finish();
p.Close();
#endregion
}
#endregion
#region EXTRACT THE ZIP FILE
public bool UnZipFile(string InputPathOfZipFile, string FileName)
{
bool ret = true;
Label1.Text = "Please wait, extracting downloaded file";
string zipDirectory = string.Empty;
try
{
#region If Folder already exist Delete it
if (Directory.Exists(Server.MapPath("~/updates/" + FileName))) // server data field
{
String[] files = Directory.GetFiles(Server.MapPath("~/updates/" + FileName));//server data field
foreach (var file in files)
File.Delete(file);
Directory.Delete(Server.MapPath("~/updates/" + FileName), true);//server data field
}
#endregion
if (File.Exists(InputPathOfZipFile))
{
string baseDirectory = Path.GetDirectoryName(InputPathOfZipFile);
using (ZipInputStream ZipStream = new
ZipInputStream(File.OpenRead(InputPathOfZipFile)))
{
ZipEntry theEntry;
while ((theEntry = ZipStream.GetNextEntry()) != null)
{
if (theEntry.IsFile)
{
if (theEntry.Name != "")
{
string directoryName = theEntry.Name.Substring(theEntry.Name.IndexOf(FileName)); // server data field
string[] DirectorySplit = directoryName.Split('\\');
for (int i = 0; i < DirectorySplit.Length - 1; i++)
{
if (zipDirectory != null || zipDirectory != "")
zipDirectory = zipDirectory + @"\" + DirectorySplit[i];
else
zipDirectory = zipDirectory + DirectorySplit[i];
}
string first = Server.MapPath("~/updates") + @"\" + zipDirectory;
if (!Directory.Exists(first))
Directory.CreateDirectory(first);
string strNewFile = @"" + baseDirectory + @"\" + directoryName;
if (File.Exists(strNewFile))
{
continue;
}
zipDirectory = string.Empty;
using (FileStream streamWriter = File.Create(strNewFile))
{
int size = 2048;
byte[] data = new byte[2048];
while (true)
{
size = ZipStream.Read(data, 0, data.Length);
if (size > 0)
streamWriter.Write(data, 0, size);
else
break;
}
streamWriter.Close();
}
}
}
else if (theEntry.IsDirectory)
{
string strNewDirectory = @"" + baseDirectory + @"\" +
theEntry.Name;
if (!Directory.Exists(strNewDirectory))
{
Directory.CreateDirectory(strNewDirectory);
}
}
}
ZipStream.Close();
}
}
}
catch (Exception ex)
{
ret = false;
}
return ret;
}
#endregion
答案 9 :(得分:0)
我更喜欢SharpZiplib 请找到示例代码 UnZip Files in .Net C#
答案 10 :(得分:0)
我会推荐我们的http://www.rebex.net/zip.net/,但我有偏见。 Download试用并自行检查功能和样本。
答案 11 :(得分:0)
SevenZipSharp是tha 7z.dll和LZMA SDK的包装器,它是开源的,免费的。
SevenZipCompressor compressor = new SevenZipCompressor();
compressor.CompressionLevel = CompressionLevel.Ultra;
compressor.CompressionMethod = CompressionMethod.Lzma;
compressor.CompressionMode = CompressionMode.Create;
compressor.CompressFiles(...);