Skip to content

Commit

Permalink
Merge pull request #1379 from abpframework/maliming/zip
Browse files Browse the repository at this point in the history
Resolve #1114 Use the new zip class library (SharpZipLib)
  • Loading branch information
yekalkan authored Jun 26, 2019
2 parents 6a54cdc + 0505124 commit 69d76c5
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 95 deletions.
2 changes: 1 addition & 1 deletion framework/src/Volo.Abp.Cli.Core/Volo.Abp.Cli.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Ionic.Zip" Version="1.9.1.8" />
<PackageReference Include="SharpZipLib" Version="1.1.0" />
<PackageReference Include="HtmlAgilityPack.NetCore" Version="1.5.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="2.2.0" />
<PackageReference Include="NuGet.Versioning" Version="5.1.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Ionic.Zip;
using ICSharpCode.SharpZipLib.Core;
using ICSharpCode.SharpZipLib.Zip;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Volo.Abp.Cli.Args;
Expand Down Expand Up @@ -61,9 +62,33 @@ public async Task ExecuteAsync(CommandLineArgs commandLineArgs)

using (var templateFileStream = new MemoryStream(result.ZipContent))
{
using (var templateZipFile = ZipFile.Read(templateFileStream))
using (var zipInputStream = new ZipInputStream(templateFileStream))
{
templateZipFile.ExtractAll(outputFolder, ExtractExistingFileAction.Throw);
var zipEntry = zipInputStream.GetNextEntry();
while (zipEntry != null)
{
var fullZipToPath = Path.Combine(outputFolder, zipEntry.Name);
var directoryName = Path.GetDirectoryName(fullZipToPath);

if (!string.IsNullOrEmpty(directoryName))
{
Directory.CreateDirectory(directoryName);
}

var fileName = Path.GetFileName(fullZipToPath);
if (fileName.Length == 0)
{
zipEntry = zipInputStream.GetNextEntry();
continue;
}

var buffer = new byte[4096]; // 4K is optimum
using (var streamWriter = File.Create(fullZipToPath))
{
StreamUtils.Copy(zipInputStream, streamWriter, buffer);
}
zipEntry = zipInputStream.GetNextEntry();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Ionic.Zip;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
using Volo.Abp.Cli.ProjectBuilding.Files;
using Volo.Abp.Cli.ProjectBuilding.Zipping;

namespace Volo.Abp.Cli.ProjectBuilding.Building.Steps
{
Expand All @@ -13,10 +13,27 @@ public override void Execute(ProjectBuildContext context)

private static byte[] CreateZipFileFromEntries(FileEntryList entries)
{
using (var resultZipFile = new ZipFile())
using (var memoryStream = new MemoryStream())
{
entries.CopyToZipFile(resultZipFile);
return resultZipFile.GetBytes();
using (var zipOutputStream = new ZipOutputStream(memoryStream))
{
zipOutputStream.SetLevel(3); //0-9, 9 being the highest level of compression

foreach (var entry in entries)
{
zipOutputStream.PutNextEntry(new ZipEntry(entry.Name)
{
Size = entry.Bytes.Length
});
zipOutputStream.Write(entry.Bytes, 0, entry.Bytes.Length);
}

zipOutputStream.CloseEntry();
zipOutputStream.IsStreamOwner = false;
}

memoryStream.Position = 0;
return memoryStream.ToArray();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System.IO;
using Ionic.Zip;
using System;
using System.Collections.Generic;
using System.IO;
using ICSharpCode.SharpZipLib.Core;
using ICSharpCode.SharpZipLib.Zip;
using Volo.Abp.Cli.ProjectBuilding.Files;
using Volo.Abp.Cli.ProjectBuilding.Zipping;

namespace Volo.Abp.Cli.ProjectBuilding.Building.Steps
{
Expand All @@ -14,12 +16,28 @@ public override void Execute(ProjectBuildContext context)

private static FileEntryList GetEntriesFromZipFile(byte[] fileBytes)
{
var fileEntries = new List<FileEntry>();

using (var memoryStream = new MemoryStream(fileBytes))
{
using (var templateZipFile = ZipFile.Read(memoryStream))
using (var zipInputStream = new ZipInputStream(memoryStream))
{
return templateZipFile.ToFileEntryList();
var zipEntry = zipInputStream.GetNextEntry();
while (zipEntry != null)
{
var buffer = new byte[4096]; // 4K is optimum

using (var fileEntryMemoryStream = new MemoryStream())
{
StreamUtils.Copy(zipInputStream, fileEntryMemoryStream, buffer);
fileEntries.Add(new FileEntry(zipEntry.Name.EnsureStartsWith('/'), fileEntryMemoryStream.ToArray(), zipEntry.IsDirectory));
}

zipEntry = zipInputStream.GetNextEntry();
}
}

return new FileEntryList(fileEntries);
}
}
}
Expand Down

This file was deleted.

This file was deleted.

0 comments on commit 69d76c5

Please sign in to comment.