我有一个zip文件,其中包含另一个zip文件和一些其他文件。
我拿起主文件,然后使用ExtractToDirectory将其解压缩到目的地。这工作正常,我最终得到一个zip文件和一些其他XML文件。
接下来,我查看刚提取的文件,然后选择.zip文件名。如果我现在使用ExtractToDirectory,那么我会遇到问题,因为它不会使用此zip中的文件覆盖root中的文件。
如果我使用ExtractTofile,我发现可以强制覆盖,但我需要解决该目录。我以为我就是这样,但是我尝试使用代码提取第二个拉链时遇到问题。
任何想法如何提取第二个文件并覆盖现有文件?
ZipFile.ExtractToDirectory(file_name, destination);
var d = new DirectoryInfo(destination);
FileInfo[] Files = d.GetFiles("*.zip"); //Get name of zip files
foreach (FileInfo file in Files)
{
//open file
ZipArchive zipArchive = ZipFile.OpenRead(destination + "\\"+ file);
foreach (ZipArchiveEntry entry in zipArchive.Entries)
{
//If Dir does not exist then make it
if (!Directory.Exists(destination + entry.FullName))
Directory.CreateDirectory(destination + entry.FullName);
// extract files into that folder
entry.ExtractToFile(destination + entry.FullName, true);
// Show user what you are doing
rchtxtbx_output.AppendText("Extracting ..... " + entry.FullName + "\r");
rchtxtbx_output.ScrollToCaret();
}
zipArchive.Dispose();
}
答案 0 :(得分:0)
过了一会儿,我找出了什么问题。对我来说,我得到了.Name和.FullName给了我相同的结果。现在我得到.FullName并手动剥离它,这样我就可以提取我需要制作的目录。突然间一切正常。我正在使用的固定代码如下。
//If archive directory exists then delete it.
if (Directory.Exists(destination + "archive")) Directory.Delete(destination + "archive", true);
ZipFile.ExtractToDirectory(file_name, temp_destination);
//look inside that folder for another zip file containing the data we need
var d = new DirectoryInfo(temp_destination);
FileInfo[] Files = d.GetFiles("*.zip"); //Get txt files
foreach (FileInfo file in Files)
{
new Thread(() =>
{
Thread.CurrentThread.IsBackground = true;
ZipArchive zipArchive = ZipFile.OpenRead(temp_destination + file.Name);
foreach (ZipArchiveEntry entry in zipArchive.Entries)
{
string completeFileName = entry.FullName;
int idx = completeFileName.LastIndexOf('/');
if (completeFileName.Contains('/'))
{
string foldername = completeFileName.Substring(0, idx);
if (!Directory.Exists(destination + foldername))
Directory.CreateDirectory(destination + foldername);
}
entry.ExtractToFile(Path.Combine(destination, entry.FullName), true);
Invoke(new Action(() =>
{
rchtxtbx_output.AppendText("Extracting ..... " + entry.FullName);
rchtxtbx_output.AppendText(Environment.NewLine);
rchtxtbx_output.ScrollToCaret();
Cursor.Current = Cursors.WaitCursor;
}));
}
zipArchive.Dispose();