Class ZipUtility
Provides utility methods for compressing and decompressing zip files.
Inherited Members
Namespace: Unity.SharpZipLib .Utils
Assembly: Unity.SharpZipLib.Utils.dll
Syntax
public static class ZipUtility
Remarks
This class provides facilities to compress entire folders into a zip file and to uncompress a zip file into folders.
Examples
The following example adds a menu item named "CompressAndUncompress" to Debug in the menu bar that compresses the Assets folder to UnityAssets.zip and extracts the contents back.
using System.IO;
using Unity.SharpZipLib.Utils;
using UnityEditor;
using UnityEngine;
public static class DebugMenu {
[MenuItem("Debug/CompressAndUncompress")]
static void CompressAndUncompress() {
//Compress
string folderToCompress = "Assets";
string zipPath = Path.Combine(Application.temporaryCachePath, "UnityAssets.zip");
ZipUtility.CompressFolderToZip(zipPath,null, folderToCompress);
Debug.Log($"{folderToCompress} folder compressed to: " + zipPath);
//Uncompress
string extractPath = Path.Combine(Application.temporaryCachePath, "UnityAssetsExtracted");
ZipUtility.UncompressFromZip(zipPath, null, extractPath);
Debug.Log($"Uncompressed to: " + extractPath);
}
}
Methods
CompressFolderToZip(string, string, string)
Creates a zip file on disk containing the contents of the nominated folder.
Declaration
public static void CompressFolderToZip(string outPathname, string password, string folderName)
Parameters
Type | Name | Description |
---|---|---|
string | outPathname | The path where the created zip file will be saved |
string | password | The password required to open the zip file. Set to |
string | folderName | The folder to be compressed |
Remarks
This method recursively compresses all files within the specified folder. You can specify a password to encrypt the target zip file.
Examples
/// <summary>
/// Compresses all files and directories within the temporary cache path into a zip file, and saves it in the persistent data path.
/// </summary>
/// <param name="outputZipFileName">The name of the output zip file, with its extension</param>
/// <returns>The full file path to the created zip archive in the persistent data directory.</returns>
private string CompressTemporaryCacheToPersistentData(string outputZipFileName) {
string outputZipPath = Path.Combine(Application.persistentDataPath, outputZipFileName);
ZipUtility.CompressFolderToZip(outputZipPath,null, Application.temporaryCachePath);
return outputZipPath;
}
UncompressFromZip(string, string, string)
Uncompress the contents of a zip file into the specified folder.
Declaration
public static void UncompressFromZip(string archivePath, string password, string outFolder)
Parameters
Type | Name | Description |
---|---|---|
string | archivePath | The path to the zip file to be extracted |
string | password | The password required to open the zip file. Set to |
string | outFolder | The output folder where the contents will be extracted |
Remarks
If the output folder already exists, its contents will be deleted before the extraction.
Examples
/// <summary>
/// Extracts the contents of a specified zip file from the persistent data path to a designated folder within the temporary cache directory.
/// </summary>
/// <param name="persistentDataZipFileName">The name of the zip file, with extension, located in the persistent data directory</param>
/// <param name="tempCacheFolderName">The name of the folder within the temporary cache where the zip file's contents will be extracted</param>
/// <returns>The full file path to the root folder containing the extracted files in the temporary cache directory.</returns>
private string ExtractPersistentDataZipToTemporaryCache(string persistentDataZipFileName, string tempCacheFolderName) {
string zipPath = Path.Combine(Application.persistentDataPath, persistentDataZipFileName);
string extractedFilesRoot = Path.Combine(Application.temporaryCachePath, tempCacheFolderName);
ZipUtility.UncompressFromZip(zipPath, null, extractedFilesRoot);
return extractedFilesRoot;
}