Skip to content

Commit

Permalink
feat: Add SharpZipLib back with different namespace for internal use …
Browse files Browse the repository at this point in the history
…(workaround) (#162)

* Update SharpZipLib.dll with a new DLL which have a modified namespace due to a conflict with an internal package (temporary workaround)
  • Loading branch information
sindharta authored May 7, 2020
1 parent 5fbb7b5 commit 88c7b37
Show file tree
Hide file tree
Showing 6 changed files with 214 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Runtime/SharpZipLib.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added Runtime/SharpZipLib/SharpZipLib.dll
Binary file not shown.
33 changes: 33 additions & 0 deletions Runtime/SharpZipLib/SharpZipLib.dll.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

141 changes: 141 additions & 0 deletions Runtime/SharpZipLib/ZipUtility.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
using ICSharpCode.SharpZipLib.Core;
using ICSharpCode.SharpZipLib.Zip;
using System;
using System.IO;

namespace Unity.SharpZipLib.Utils {

//[TODO-sin: 2020-4-30] Move this to another package
internal static class ZipUtility {

public static void UncompressFromZip(string archivePath, string password, string outFolder) {
if (Directory.Exists(outFolder)) {
Directory.Delete(outFolder,true);
}

Directory.CreateDirectory(outFolder);

using(Stream fs = File.OpenRead(archivePath))
using(ZipFile zf = new ZipFile(fs)){

if (!String.IsNullOrEmpty(password)) {
// AES encrypted entries are handled automatically
zf.Password = password;
}

foreach (ZipEntry zipEntry in zf) {
if (!zipEntry.IsFile) {
// Ignore directories
continue;
}
String entryFileName = zipEntry.Name;
// to remove the folder from the entry:
//entryFileName = Path.GetFileName(entryFileName);
// Optionally match entrynames against a selection list here
// to skip as desired.
// The unpacked length is available in the zipEntry.Size property.

// Manipulate the output filename here as desired.
var fullZipToPath = Path.Combine(outFolder, entryFileName);
var directoryName = Path.GetDirectoryName(fullZipToPath);
if (directoryName.Length > 0) {
Directory.CreateDirectory(directoryName);
}

// 4K is optimum
var buffer = new byte[4096];

// Unzip file in buffered chunks. This is just as fast as unpacking
// to a buffer the full size of the file, but does not waste memory.
// The "using" will close the stream even if an exception occurs.
using(Stream zipStream = zf.GetInputStream(zipEntry))
using (Stream fsOutput = File.Create(fullZipToPath)) {
StreamUtils.Copy(zipStream, fsOutput, buffer);
}
}
}
}

//---------------------------------------------------------------------------------------------------------------------

// Compresses the files in the nominated folder, and creates a zip file on disk named as outPathname.
public static void CompressFolderToZip(string outPathname, string password, string folderName) {

using(FileStream fsOut = File.Create(outPathname))
using(var zipStream = new ZipOutputStream(fsOut)) {

//0-9, 9 being the highest level of compression
zipStream.SetLevel(3);

// optional. Null is the same as not setting. Required if using AES.
zipStream.Password = password;

// This setting will strip the leading part of the folder path in the entries,
// to make the entries relative to the starting folder.
// To include the full path for each entry up to the drive root, assign to 0.
int folderOffset = folderName.Length + (folderName.EndsWith("\\") ? 0 : 1);

CompressFolderToZipInternal(folderName, zipStream, folderOffset);

}

}

//---------------------------------------------------------------------------------------------------------------------

// Recursively compresses a folder structure
private static void CompressFolderToZipInternal(string path, ZipOutputStream zipStream, int folderOffset) {

var files = Directory.GetFiles(path);

foreach (var filename in files) {

var fi = new FileInfo(filename);

// Make the name in zip based on the folder
var entryName = filename.Substring(folderOffset);

// Remove drive from name and fixe slash direction
entryName = ZipEntry.CleanName(entryName);

var newEntry = new ZipEntry(entryName);

// Note the zip format stores 2 second granularity
newEntry.DateTime = fi.LastWriteTime;

// Specifying the AESKeySize triggers AES encryption.
// Allowable values are 0 (off), 128 or 256.
// A password on the ZipOutputStream is required if using AES.
// newEntry.AESKeySize = 256;

// To permit the zip to be unpacked by built-in extractor in WinXP and Server2003,
// WinZip 8, Java, and other older code, you need to do one of the following:
// Specify UseZip64.Off, or set the Size.
// If the file may be bigger than 4GB, or you do not need WinXP built-in compatibility,
// you do not need either, but the zip will be in Zip64 format which
// not all utilities can understand.
// zipStream.UseZip64 = UseZip64.Off;
newEntry.Size = fi.Length;

zipStream.PutNextEntry(newEntry);

// Zip the file in buffered chunks
// the "using" will close the stream even if an exception occurs
var buffer = new byte[4096];
using (FileStream fsInput = File.OpenRead(filename)) {
StreamUtils.Copy(fsInput, zipStream, buffer);
}
zipStream.CloseEntry();
}

// Recursively call CompressFolder on all folders in path
var folders = Directory.GetDirectories(path);
foreach (var folder in folders) {
CompressFolderToZipInternal(folder, zipStream, folderOffset);
}
}

}

} //end namespace

11 changes: 11 additions & 0 deletions Runtime/SharpZipLib/ZipUtility.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions Third Party Notices.md
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,24 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
```

# SharpZipLib
```
Copyright © 2000-2018 SharpZipLib Contributors
Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the "Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
```

0 comments on commit 88c7b37

Please sign in to comment.