Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove dependency on SharpZipLib #5127

Merged
merged 10 commits into from
Jun 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 58 additions & 12 deletions DNN Platform/Library/Common/Utilities/FileSystemExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,64 @@
// See the LICENSE file in the project root for more information

namespace DotNetNuke.Common.Utilities
{
using System;
{
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;

public static class FileSystemExtensions
{
public static void CheckZipEntry(this ICSharpCode.SharpZipLib.Zip.ZipEntry input)
{
var fullName = input.Name.Replace('\\', '/');
if (fullName.StartsWith("..") || fullName.Contains("/../"))
{
throw new Exception("Illegal Zip File");
}
}
public static class FileSystemExtensions
{
public static void CheckZipEntry(this ZipArchiveEntry input)
{
var fullName = input.FullName.Replace('\\', '/');
if (fullName.StartsWith("..") || fullName.Contains("/../"))
{
throw new Exception("Illegal Zip File");
}
}

public static string ReadTextFile(this ZipArchiveEntry input)
{
var text = string.Empty;
using (var reader = new StreamReader(input.Open()))
{
text = reader.ReadToEnd();
}

return text;
}

public static void CopyToStream(this Stream strIn, Stream strOut, int bufferSize)
{
var buffer = new byte[bufferSize];
int bytesRead;
while ((bytesRead = strIn.Read(buffer, 0, buffer.Length)) > 0)
{
strOut.Write(buffer, 0, bytesRead);
}
}

public static IEnumerable<ZipArchiveEntry> FileEntries(this ZipArchive zip)
{
foreach (var entry in zip.Entries)
{
entry.CheckZipEntry();
if (!string.IsNullOrEmpty(entry.Name))
{
yield return entry;
}
}
}

[Obsolete("Deprecated in 9.11.0, will be removed in 11.0.0, replaced with .net compression types.")]
public static void CheckZipEntry(this ICSharpCode.SharpZipLib.Zip.ZipEntry input)
{
var fullName = input.Name.Replace('\\', '/');
if (fullName.StartsWith("..") || fullName.Contains("/../"))
{
throw new Exception("Illegal Zip File");
}
}
}
}
234 changes: 167 additions & 67 deletions DNN Platform/Library/Common/Utilities/FileSystemUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
namespace DotNetNuke.Common.Utilities
{
using System;
using System.IO;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Threading;
using System.Web;
Expand All @@ -14,12 +15,12 @@ namespace DotNetNuke.Common.Utilities
using DotNetNuke.Instrumentation;
using DotNetNuke.Services.FileSystem;
using DotNetNuke.Services.Localization;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Zip;

using Directory = SchwabenCode.QuickIO.QuickIODirectory;
using DirectoryInfo = SchwabenCode.QuickIO.QuickIODirectoryInfo;
using File = SchwabenCode.QuickIO.QuickIOFile;
using FileInfo = DotNetNuke.Services.FileSystem.FileInfo;
using FileInfo = Services.FileSystem.FileInfo;

public class FileSystemUtils
{
Expand All @@ -30,7 +31,7 @@ public class FileSystemUtils
/// Adds a File to a Zip File.
/// </summary>
/// -----------------------------------------------------------------------------
public static void AddToZip(ref ZipOutputStream ZipFile, string filePath, string fileName, string folder)
public static void AddToZip(ref ZipArchive ZipFile, string filePath, string fileName, string folder)
{
FileStream fs = null;
try
Expand All @@ -50,14 +51,14 @@ public static void AddToZip(ref ZipOutputStream ZipFile, string filePath, string
}

// Create Zip Entry
var entry = new ZipEntry(Path.Combine(folder, fileName));
entry.DateTime = DateTime.Now;
entry.Size = fs.Length;
var entry = ZipFile.CreateEntry(Path.Combine(folder, fileName));
entry.LastWriteTime = DateTime.Now;
using (var zipStream = entry.Open())
{
fs.CopyToStream(zipStream, 25000);
}

fs.Close();

// Compress file and add to Zip file
ZipFile.PutNextEntry(entry);
ZipFile.Write(buffer, 0, buffer.Length);
}
finally
{
Expand Down Expand Up @@ -177,70 +178,58 @@ public static string ReadFile(string filePath)
return fileContent;
}

public static void UnzipResources(ZipInputStream zipStream, string destPath)
public static void UnzipResources(ZipArchive zipStream, string destPath)
{
try
{
var zipEntry = zipStream.GetNextEntry();
while (zipEntry != null)
{
zipEntry.CheckZipEntry();
HtmlUtils.WriteKeepAlive();
var localFileName = zipEntry.Name;
var relativeDir = Path.GetDirectoryName(zipEntry.Name);
if (!string.IsNullOrEmpty(relativeDir) && (!Directory.Exists(Path.Combine(destPath, relativeDir))))
{
Directory.Create(Path.Combine(destPath, relativeDir), true);
}

if (!zipEntry.IsDirectory && (!string.IsNullOrEmpty(localFileName)))
{
var fileNamePath = FixPath(Path.Combine(destPath, localFileName));
try
{
if (File.Exists(fileNamePath))
{
File.SetAttributes(fileNamePath, FileAttributes.Normal);
File.Delete(fileNamePath);
}

FileStream objFileStream = null;
try
{
File.Create(fileNamePath);
objFileStream = File.Open(fileNamePath);
int intSize = 2048;
var arrData = new byte[2048];
intSize = zipStream.Read(arrData, 0, arrData.Length);
while (intSize > 0)
{
objFileStream.Write(arrData, 0, intSize);
intSize = zipStream.Read(arrData, 0, arrData.Length);
}
}
finally
{
if (objFileStream != null)
{
objFileStream.Close();
objFileStream.Dispose();
}
}
}
catch (Exception ex)
{
Logger.Error(ex);
}
{
foreach (var zipEntry in zipStream.FileEntries())
{
HtmlUtils.WriteKeepAlive();
var localFileName = zipEntry.FullName;
var relativeDir = Path.GetDirectoryName(zipEntry.FullName);
if (!string.IsNullOrEmpty(relativeDir) && (!Directory.Exists(Path.Combine(destPath, relativeDir))))
{
Directory.Create(Path.Combine(destPath, relativeDir), true);
}

if (!string.IsNullOrEmpty(localFileName))
{
var fileNamePath = FixPath(Path.Combine(destPath, localFileName));
try
{
if (File.Exists(fileNamePath))
{
File.SetAttributes(fileNamePath, FileAttributes.Normal);
File.Delete(fileNamePath);
}

FileStream objFileStream = null;
try
{
File.Create(fileNamePath);
objFileStream = File.Open(fileNamePath);
zipEntry.Open().CopyToStream(objFileStream, 25000);
}
finally
{
if (objFileStream != null)
{
objFileStream.Close();
objFileStream.Dispose();
}
}
}
catch (Exception ex)
{
Logger.Error(ex);
}
}

zipEntry = zipStream.GetNextEntry();
}
}
finally
{
if (zipStream != null)
{
zipStream.Close();
zipStream.Dispose();
}
}
Expand Down Expand Up @@ -546,6 +535,117 @@ private static void WriteStream(HttpResponse objResponse, Stream objStream)
objStream.Dispose();
}
}
}
}


[Obsolete("Deprecated in 9.11.0, will be removed in 11.0.0, replaced with .net compression types.")]
public static void AddToZip(ref ZipOutputStream ZipFile, string filePath, string fileName, string folder)
{
FileStream fs = null;
try
{
// Open File Stream
fs = File.OpenRead(FixPath(filePath));

// Read file into byte array buffer
var buffer = new byte[fs.Length];

var len = fs.Read(buffer, 0, buffer.Length);
if (len != fs.Length)
{
Logger.ErrorFormat(
"Reading from " + filePath + " didn't read all data in buffer. " +
"Requested to read {0} bytes, but was read {1} bytes", fs.Length, len);
}

// Create Zip Entry
var entry = new ZipEntry(Path.Combine(folder, fileName));
entry.DateTime = DateTime.Now;
entry.Size = fs.Length;
fs.Close();

// Compress file and add to Zip file
ZipFile.PutNextEntry(entry);
ZipFile.Write(buffer, 0, buffer.Length);
}
finally
{
if (fs != null)
{
fs.Close();
fs.Dispose();
}
}
}

[Obsolete("Deprecated in 9.11.0, will be removed in 11.0.0, replaced with .net compression types.")]
public static void UnzipResources(ZipInputStream zipStream, string destPath)
{
try
{
var zipEntry = zipStream.GetNextEntry();
while (zipEntry != null)
{
zipEntry.CheckZipEntry();
HtmlUtils.WriteKeepAlive();
var localFileName = zipEntry.Name;
var relativeDir = Path.GetDirectoryName(zipEntry.Name);
if (!string.IsNullOrEmpty(relativeDir) && (!Directory.Exists(Path.Combine(destPath, relativeDir))))
{
Directory.Create(Path.Combine(destPath, relativeDir), true);
}

if (!zipEntry.IsDirectory && (!string.IsNullOrEmpty(localFileName)))
{
var fileNamePath = FixPath(Path.Combine(destPath, localFileName));
try
{
if (File.Exists(fileNamePath))
{
File.SetAttributes(fileNamePath, FileAttributes.Normal);
File.Delete(fileNamePath);
}

FileStream objFileStream = null;
try
{
File.Create(fileNamePath);
objFileStream = File.Open(fileNamePath);
int intSize = 2048;
var arrData = new byte[2048];
intSize = zipStream.Read(arrData, 0, arrData.Length);
while (intSize > 0)
{
objFileStream.Write(arrData, 0, intSize);
intSize = zipStream.Read(arrData, 0, arrData.Length);
}
}
finally
{
if (objFileStream != null)
{
objFileStream.Close();
objFileStream.Dispose();
}
}
}
catch (Exception ex)
{
Logger.Error(ex);
}
}

zipEntry = zipStream.GetNextEntry();
}
}
finally
{
if (zipStream != null)
{
zipStream.Close();
zipStream.Dispose();
}
}
}
}
}
2 changes: 2 additions & 0 deletions DNN Platform/Library/DotNetNuke.Library.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.Design" />
<Reference Include="System.Drawing" />
<Reference Include="System.IO.Compression" />
<Reference Include="System.IO.Compression.FileSystem" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.Security" />
Expand Down
Loading