如何在 C# 中读取多个 zip 文件
How to read multi zip files in c#
我有两个问题,你能帮我吗,我有一个可以漂亮地读取 zip 文件的功能,但我不能再读取拆分的 zip 文件了。
zip.001
邮编.002
如何读取zip.001文件内容?以及是否可以阅读带有特殊字符的内容,例如ó ć ę 等
提前感谢您的帮助。
问候
罗伯斯
using (OpenFileDialog openFileDialog1 = new OpenFileDialog())
{
openFileDialog1.InitialDirectory = d.Name;
//openFileDialog1.InitialDirectory = @"c:\";
openFileDialog1.Filter = "zip files (*.zip)|*.zip |All files (*.*)|*.*";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
ZipArchive zip = ZipFile.OpenRead(openFileDialog1.FileName);
listBox1.Items.Clear();
foreach (ZipArchiveEntry entry in zip.Entries)
{
string unicodeString = entry.FullName;
// Create two different encodings.
Encoding ascii = Encoding.GetEncoding("CP852");
Encoding unicode = Encoding.Unicode;
// Convert the string into a byte array.
byte[] unicodeBytes = unicode.GetBytes(unicodeString);
// Perform the conversion from one encoding to the other.
byte[] asciiBytes = Encoding.Convert(unicode, ascii, unicodeBytes);
listBox1.Items.Add(unicodeString);
listBox1.ForeColor = Color.FromArgb(1, 150, 227);
//textBox5.AppendText(entry.FullName);
// toolStripStatusLabel2.Text = " Ilość elementów: " + listBox1.Items.Count.ToString();
}
}
}
只要您的 zip 文件不是使用 7-zip 创建的并且遵循文件名 filename.z01
、filenamez02
、...[=15=,您就可以使用 DotNetZip ]
using (ZipFile zip = ZipFile.Read(NameOfExistingZipFile))
{
foreach (ZipEntry e in zip)
{
Console.WriteLine($"{e.FileName} {e.LastModified}");
}
}
另一种可能性是 SevenZipSharp,但它也需要安装 7-zip .DLL。
SevenZipSharp 及其只读 7zip 文件而不是 zip 文件,我收到错误。
using (OpenFileDialog openFileDialog1 = new OpenFileDialog())
{
openFileDialog1.InitialDirectory = d.Name;
openFileDialog1.Filter = "zip files (*.zip)|*.zip |All files (*.*)|*.*";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
SevenZip.SevenZipCompressor.SetLibraryPath(Path.Combine(Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory + "X86\"),"7z.dll"));
SevenZip.SevenZipExtractor.SetLibraryPath(Path.Combine(Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory + "X86\"), "7z.dll"));
using (var zip = new SevenZipExtractor(openFileDialog1.FileName))
{
foreach (var file in zip.ArchiveFileData)
{
listBox1.Items.Add(file.FileName);
listBox1.ForeColor = Color.FromArgb(1, 150, 227);
}
}
}
}
我以不同的方式做到了,不幸的是我不得不将“zip”作为链接文件放在一边,并专注于链接的“rar”,它读取超级,所以“zip”仅读取为带有 DotNetZip 的 zip,并且归档文件分成几个部分,由 rar 读取,完全由:SharpCompress。
if (ext == ".rar")
{
//------------------------------------------------------------------------------
var archive = ArchiveFactory.Open(item);
foreach (var entry in archive.Entries)
{
if (!entry.IsDirectory)
{
// Console.WriteLine(entry.Key);
listBox1.Items.Add(entry.Key);
listBox1.ForeColor = Color.White;
}
}
//------------------------------------------------------------------------------
}
else if (ext == ".zip")
{
using (ZipFile zip = new ZipFile(item))
{
zip.AlternateEncoding = System.Text.Encoding.GetEncoding("windows-1250");
// Loop through the archive's files.
foreach (ZipEntry zip_entry in zip)
{
listBox1.Items.Add(zip_entry.FileName);
listBox1.ForeColor = Color.White;
}
}
}
我有两个问题,你能帮我吗,我有一个可以漂亮地读取 zip 文件的功能,但我不能再读取拆分的 zip 文件了。
zip.001 邮编.002
如何读取zip.001文件内容?以及是否可以阅读带有特殊字符的内容,例如ó ć ę 等
提前感谢您的帮助。
问候 罗伯斯
using (OpenFileDialog openFileDialog1 = new OpenFileDialog())
{
openFileDialog1.InitialDirectory = d.Name;
//openFileDialog1.InitialDirectory = @"c:\";
openFileDialog1.Filter = "zip files (*.zip)|*.zip |All files (*.*)|*.*";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
ZipArchive zip = ZipFile.OpenRead(openFileDialog1.FileName);
listBox1.Items.Clear();
foreach (ZipArchiveEntry entry in zip.Entries)
{
string unicodeString = entry.FullName;
// Create two different encodings.
Encoding ascii = Encoding.GetEncoding("CP852");
Encoding unicode = Encoding.Unicode;
// Convert the string into a byte array.
byte[] unicodeBytes = unicode.GetBytes(unicodeString);
// Perform the conversion from one encoding to the other.
byte[] asciiBytes = Encoding.Convert(unicode, ascii, unicodeBytes);
listBox1.Items.Add(unicodeString);
listBox1.ForeColor = Color.FromArgb(1, 150, 227);
//textBox5.AppendText(entry.FullName);
// toolStripStatusLabel2.Text = " Ilość elementów: " + listBox1.Items.Count.ToString();
}
}
}
只要您的 zip 文件不是使用 7-zip 创建的并且遵循文件名 filename.z01
、filenamez02
、...[=15=,您就可以使用 DotNetZip ]
using (ZipFile zip = ZipFile.Read(NameOfExistingZipFile))
{
foreach (ZipEntry e in zip)
{
Console.WriteLine($"{e.FileName} {e.LastModified}");
}
}
另一种可能性是 SevenZipSharp,但它也需要安装 7-zip .DLL。
SevenZipSharp 及其只读 7zip 文件而不是 zip 文件,我收到错误。
using (OpenFileDialog openFileDialog1 = new OpenFileDialog())
{
openFileDialog1.InitialDirectory = d.Name;
openFileDialog1.Filter = "zip files (*.zip)|*.zip |All files (*.*)|*.*";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
SevenZip.SevenZipCompressor.SetLibraryPath(Path.Combine(Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory + "X86\"),"7z.dll"));
SevenZip.SevenZipExtractor.SetLibraryPath(Path.Combine(Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory + "X86\"), "7z.dll"));
using (var zip = new SevenZipExtractor(openFileDialog1.FileName))
{
foreach (var file in zip.ArchiveFileData)
{
listBox1.Items.Add(file.FileName);
listBox1.ForeColor = Color.FromArgb(1, 150, 227);
}
}
}
}
我以不同的方式做到了,不幸的是我不得不将“zip”作为链接文件放在一边,并专注于链接的“rar”,它读取超级,所以“zip”仅读取为带有 DotNetZip 的 zip,并且归档文件分成几个部分,由 rar 读取,完全由:SharpCompress。
if (ext == ".rar")
{
//------------------------------------------------------------------------------
var archive = ArchiveFactory.Open(item);
foreach (var entry in archive.Entries)
{
if (!entry.IsDirectory)
{
// Console.WriteLine(entry.Key);
listBox1.Items.Add(entry.Key);
listBox1.ForeColor = Color.White;
}
}
//------------------------------------------------------------------------------
}
else if (ext == ".zip")
{
using (ZipFile zip = new ZipFile(item))
{
zip.AlternateEncoding = System.Text.Encoding.GetEncoding("windows-1250");
// Loop through the archive's files.
foreach (ZipEntry zip_entry in zip)
{
listBox1.Items.Add(zip_entry.FileName);
listBox1.ForeColor = Color.White;
}
}
}