From 937e2152797046d3f337d7919e5654a4af04565f Mon Sep 17 00:00:00 2001 From: yangxuilyx Date: Mon, 13 Jan 2025 18:08:03 +0800 Subject: [PATCH] fix zip directory error --- .../Compress/ZipCompressionStrategy.cs | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/c#/GeneralUpdate.Common/Compress/ZipCompressionStrategy.cs b/src/c#/GeneralUpdate.Common/Compress/ZipCompressionStrategy.cs index b1604c4..d4f463e 100644 --- a/src/c#/GeneralUpdate.Common/Compress/ZipCompressionStrategy.cs +++ b/src/c#/GeneralUpdate.Common/Compress/ZipCompressionStrategy.cs @@ -44,7 +44,7 @@ public void Compress(string sourceDirectoryName foreach (var toZipFileKey in toZipFileDictionaryList.Keys) { if (toZipFileKey == destinationArchiveFileName) continue; - + var toZipedFileName = Path.GetFileName(toZipFileKey); var toDelArchives = new List(); foreach (var zipArchiveEntry in archive.Entries) @@ -84,8 +84,8 @@ public void Compress(string sourceDirectoryName var toDelArchives = new List(); foreach (var zipArchiveEntry in archive.Entries) { - if (toZipedFileName != null - && (zipArchiveEntry.FullName.StartsWith(toZipedFileName)|| toZipedFileName.StartsWith(zipArchiveEntry.FullName))) + if (toZipedFileName != null + && (zipArchiveEntry.FullName.StartsWith(toZipedFileName) || toZipedFileName.StartsWith(zipArchiveEntry.FullName))) { toDelArchives.Add(zipArchiveEntry); } @@ -101,13 +101,13 @@ public void Compress(string sourceDirectoryName } } } - catch(Exception exception) + catch (Exception exception) { Debug.WriteLine(exception); throw new Exception($"Failed to compress archive: {exception.Message}"); } } - + /// /// Unzip the Zip file and save it to the specified target path folder . /// @@ -120,7 +120,7 @@ public void Decompress(string zipFilePath, string unZipDir, Encoding encoding) { var dirSeparatorChar = Path.DirectorySeparatorChar.ToString(); unZipDir = unZipDir.EndsWith(dirSeparatorChar) ? unZipDir : unZipDir + dirSeparatorChar; - + var directoryInfo = new DirectoryInfo(unZipDir); if (!directoryInfo.Exists) { @@ -138,14 +138,14 @@ public void Decompress(string zipFilePath, string unZipDir, Encoding encoding) for (int i = 0; i < archive.Entries.Count; i++) { var entries = archive.Entries[i]; - if (entries.FullName.EndsWith(dirSeparatorChar)) + var pattern = $"^{dirSeparatorChar}*"; + var entryFilePath = Regex.Replace(entries.FullName.Replace("/", dirSeparatorChar), pattern, + ""); + if (entryFilePath.EndsWith(dirSeparatorChar)) { continue; } - var pattern = $"^{dirSeparatorChar}*"; - var entryFilePath = Regex.Replace(entries.FullName.Replace("/", dirSeparatorChar), pattern, - ""); var filePath = directoryInfo + entryFilePath; var greatFolder = Directory.GetParent(filePath); if (greatFolder is not null && !greatFolder.Exists)