-
Notifications
You must be signed in to change notification settings - Fork 12
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
Unity 2020.3.27f1 #8
Comments
Instead of compiling the project, drag and drop the Then, build for UWP. UWP does not have access everywhere, so ensure you are accessing files within the app's local storage ( using System;
using System.IO;
using System.IO.Compression;
namespace LightBuzz.Archiver
{
/// <summary>
/// Compresses and decompresses single files and folders.
/// </summary>
public static class Archiver
{
public static readonly string Extension = ".zip";
/// <summary>
/// Compresses a folder (including its subfolders) or file.
/// </summary>
/// <param name="source">The fodler or file to compress.</param>
/// <param name="destination">The compressed zip file.</param>
/// <param name="replaceExisting">Choose whether you'll replace the file if it already exists.</param>
public static void Compress(string source, string destination, bool replaceExisting = true)
{
if (string.IsNullOrEmpty(source))
{
throw new ArgumentNullException("Source path is null or empty.");
}
if (string.IsNullOrEmpty(destination))
{
throw new ArgumentNullException("Destination path is null or empty.");
}
if (Path.GetExtension(destination) != Extension)
{
throw new ArgumentException("Destination path is not valid. You need to specify the name of a zip file, instead.");
}
if (File.Exists(destination) && replaceExisting)
{
File.Delete(destination);
}
using (MemoryStream zipMemoryStream = new MemoryStream())
{
using (ZipArchive zipArchive = new ZipArchive(zipMemoryStream, ZipArchiveMode.Create))
{
foreach (string fileToCompress in Directory.GetFiles(source))
{
byte[] buffer = File.ReadAllBytes(fileToCompress);
ZipArchiveEntry entry = zipArchive.CreateEntry(Path.GetFileName(fileToCompress));
using (Stream entryStream = entry.Open())
{
entryStream.Write(buffer, 0, buffer.Length);
}
}
}
using (FileStream fs = new FileStream(destination, FileMode.Create, FileAccess.ReadWrite))
{
byte[] array = zipMemoryStream.ToArray();
fs.Write(array, 0, array.Length);
}
}
}
/// <summary>
/// Compresses a folder, including all of its files and sub-folders.
/// </summary>
/// <param name="source">The folder containing the files to compress.</param>
/// <param name="destination">The compressed zip file.</param>
/// <param name="replaceExisting">Choose whether you'll replace the file if it already exists.</param>
public static void Compress(DirectoryInfo source, FileInfo destination, bool replaceExisting = true)
{
if (source == null)
{
throw new ArgumentNullException("Source directory is null.");
}
if (destination == null)
{
throw new ArgumentNullException("Destination file is null.");
}
Compress(source.FullName, destination.FullName, replaceExisting);
}
/// <summary>
/// Compresses a single file.
/// </summary>
/// <param name="source">The file to compress.</param>
/// <param name="destination">The compressed zip file.</param>
/// <param name="replaceExisting">Choose whether you'll replace the file if it already exists.</param>
public static void Compress(FileInfo source, FileInfo destination, bool replaceExisting)
{
if (source == null)
{
throw new ArgumentNullException("Source directory is null.");
}
if (destination == null)
{
throw new ArgumentNullException("Destination file is null.");
}
Compress(source.FullName, destination.FullName, replaceExisting);
}
/// <summary>
/// Decompresses the specified file to the specified folder.
/// </summary>
/// <param name="source">The compressed zip file.</param>
/// <param name="destination">The directory where the file will be decompressed.</param>
/// <param name="overwrite">Whether or not to extract the ZIP overwriting all files. Defaults to false, where it will throw an exception if a file already exists.</param>
public static void Decompress(string source, string destination, bool overwrite = false)
{
if (string.IsNullOrEmpty(source))
{
throw new ArgumentNullException("Source file path is null or empty.");
}
if (string.IsNullOrEmpty(destination))
{
throw new ArgumentNullException("Destination path is null or empty.");
}
if (File.GetAttributes(source).HasFlag(FileAttributes.Directory))
{
throw new ArgumentException("Source is a directory. You need to specify the name of a zip file, instead.");
}
if (!File.Exists(source))
{
throw new FileNotFoundException("Source file does not exist.");
}
if (!Directory.Exists(destination))
{
Directory.CreateDirectory(destination);
}
using (MemoryStream zipMemoryStream = new MemoryStream(File.ReadAllBytes(source)))
{
using (ZipArchive zipArchive = new ZipArchive(zipMemoryStream, ZipArchiveMode.Read))
{
foreach (ZipArchiveEntry entry in zipArchive.Entries)
{
using (Stream entryStream = entry.Open())
{
byte[] buffer = new byte[entry.Length];
entryStream.Read(buffer, 0, buffer.Length);
string destFile = Path.Combine(destination, entry.Name);
File.WriteAllBytes(destFile, buffer);
}
}
}
}
}
/// <summary>
/// Decompresses the specified file to the specified folder.
/// </summary>
/// <param name="source">The compressed zip file.</param>
/// <param name="destination">The directory where the file will be decompressed.</param>
/// <param name="overwrite">Whether or not to extract the ZIP overwriting all files. Defaults to false, where it will throw an exception if a file already exists.</param>
public static void Decompress(FileInfo source, DirectoryInfo destination, bool overwrite = false)
{
if (source == null)
{
throw new ArgumentNullException("Source file is null.");
}
if (destination == null)
{
throw new ArgumentNullException("Destination directory is null.");
}
Decompress(source.FullName, destination.FullName, overwrite);
}
}
} ATTENTION: IT DOES NOT SUPPORT SUB-FOLDERS |
Closed
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've compiled the .dll and added it to a Plugins folder
However, upon changing the project settings from .NET 2 to .NET 4.x, I get errors like
Assets/LightBuzz.Archiver.Unity/LightBuzz.Archiver.Unity/Archiver.cs(45,17): error CS0103: The name 'ZipFile' does not exist in the current context
Assets/LightBuzz.Archiver.Unity/LightBuzz.Archiver.Unity/Archiver.cs(157,31): error CS1061: 'ZipArchiveEntry' does not contain a definition for 'ExtractToFile' and no accessible extension method 'ExtractToFile' accepting a first argument of type 'ZipArchiveEntry' could be found (are you missing a using directive or an assembly reference?)
The text was updated successfully, but these errors were encountered: