Skip to content

Commit

Permalink
feat(IZipArchiveService): auto create zip folder (#4469)
Browse files Browse the repository at this point in the history
* feat: 增加文件夹自动创建功能

* test: 增加单元测试

* chore: bump version 8.10.3
  • Loading branch information
ArgoZhang authored Oct 16, 2024
1 parent c31b481 commit 5749ff0
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/BootstrapBlazor/BootstrapBlazor.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">

<PropertyGroup>
<Version>8.10.3-beta03</Version>
<Version>8.10.3</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
5 changes: 5 additions & 0 deletions src/BootstrapBlazor/Services/DefaultZipArchiveService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ public async Task ArchiveDirectory(string archiveFileName, string directoryName,
{
if (Directory.Exists(directoryName))
{
var folder = Path.GetDirectoryName(archiveFileName);
if (!string.IsNullOrEmpty(folder) && !Directory.Exists(folder))
{
Directory.CreateDirectory(folder);
}
await Task.Run(() => ZipFile.CreateFromDirectory(directoryName, archiveFileName, compressionLevel, includeBaseDirectory, encoding));
}
}
Expand Down
14 changes: 9 additions & 5 deletions test/UnitTest/Services/ZipArchiveServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,21 @@ public async Task Archive_Ok()
archService.ExtractToDirectory(archiveFile, destFolder);
Assert.True(Directory.Exists(destFolder));

// 打包文件夹
//stream = await archService.ArchiveDirectory(destFolder);
//Assert.NotNull(stream);

var destFile = Path.Combine(root, "folder.zip");
// 打包文件夹单元测试
var tempFolder = Path.Combine(root, "test_temp");
if (Directory.Exists(tempFolder))
{
Directory.Delete(tempFolder, true);
}
var destFile = Path.Combine(tempFolder, "folder.zip");
if (File.Exists(destFile))
{
File.Delete(destFile);
}
await archService.ArchiveDirectory(destFile, destFolder, includeBaseDirectory: true);
Assert.True(File.Exists(destFile));
File.Delete(destFile);

await Assert.ThrowsAsync<ArgumentNullException>(() => archService.ArchiveDirectory(null!, destFolder, includeBaseDirectory: true));
}
}

0 comments on commit 5749ff0

Please sign in to comment.