188 lines
6.0 KiB
C#
188 lines
6.0 KiB
C#
using System.Collections.Generic;
|
||
using System.IO;
|
||
using System.IO.Compression;
|
||
using UnityEngine;
|
||
|
||
public static class Extend
|
||
{
|
||
/// <summary>
|
||
/// 将流转成字节数组
|
||
/// </summary>
|
||
/// <param name="stream">文件流</param>
|
||
/// <returns></returns>
|
||
public static byte[] ToBytes(this Stream stream)
|
||
{
|
||
byte[] bytes = new byte[stream.Length];
|
||
stream.Read(bytes, 0, bytes.Length);
|
||
// 设置当前流的位置为流的开始
|
||
stream.Seek(0, SeekOrigin.Begin);
|
||
return bytes;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取文件的内容(字节数组)
|
||
/// </summary>
|
||
/// <param name="absolutePath">绝对路径</param>
|
||
/// <returns></returns>
|
||
public static byte[] GetBytesFromPath(string absolutePath)
|
||
{
|
||
using (Stream fileStream = new FileStream(absolutePath, FileMode.OpenOrCreate))
|
||
{
|
||
return fileStream.ToBytes();
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 字节数组写入文件
|
||
/// </summary>
|
||
/// <param name="bytes">文件内容</param>
|
||
/// <param name="absolutePath">绝对路径</param>
|
||
public static void WriteToFile(this byte[] bytes, string absolutePath)
|
||
{
|
||
if (File.Exists(absolutePath))
|
||
{
|
||
File.Delete(absolutePath);
|
||
}
|
||
|
||
using (var fs = new FileStream(absolutePath, FileMode.Create))
|
||
{
|
||
fs.Write(bytes, 0, bytes.Length);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 将Zip文件描述对象打包成Zip文件,并返回字节数组
|
||
/// </summary>
|
||
/// <param name="zipFiles">Zip文件描述对象数组</param>
|
||
/// <returns></returns>
|
||
public static byte[] PackedZip(this List<ZipFile> zipFiles)
|
||
{
|
||
using (Stream memoryStream = new MemoryStream())
|
||
{
|
||
using (var zipArchive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
|
||
{
|
||
foreach (var zipFile in zipFiles)
|
||
{
|
||
zipArchive.AddFile(zipFile.RelativePath, zipFile.Content);
|
||
}
|
||
}
|
||
|
||
memoryStream.Seek(0, SeekOrigin.Begin); //这句要在using ZipArchive外面,否则压缩文件会被损坏
|
||
return memoryStream.ToBytes();
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 向Zip文件中添加文件
|
||
/// </summary>
|
||
/// <param name="zipArchive"></param>
|
||
/// <param name="relativePath">相对路径</param>
|
||
/// <param name="bytes">文件内容</param>
|
||
/// <returns></returns>
|
||
private static bool AddFile(this ZipArchive zipArchive, string relativePath, byte[] bytes)
|
||
{
|
||
try
|
||
{
|
||
ZipArchiveEntry entry = zipArchive.CreateEntry(relativePath);
|
||
using (Stream entryStream = entry.Open())
|
||
{
|
||
entryStream.Write(bytes, 0, bytes.Length);
|
||
}
|
||
|
||
return true;
|
||
}
|
||
catch
|
||
{
|
||
return false;
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 描述打包成Zip时的一个文件的信息
|
||
/// </summary>
|
||
public class ZipFile
|
||
{
|
||
/// <summary>
|
||
/// 文件相对路径,带文件名和拓展名
|
||
/// </summary>
|
||
public string RelativePath { get; set; }
|
||
/// <summary>
|
||
/// 字节数组(文件内容)
|
||
/// </summary>
|
||
public byte[] Content { get; set; }
|
||
}
|
||
|
||
/***
|
||
* Title:"基础工具" 项目
|
||
* 主题:压缩包帮助类
|
||
* Description:
|
||
* 功能:【该压缩方法来自:https://www.cnblogs.com/Chary/p/No0000DF.html】
|
||
* 1、创建 zip 存档,该文档包含指定目录的文件和子目录(单个目录)
|
||
* 2、创建 zip 存档,该存档包含指定目录的文件和目录(多个目录)
|
||
* 3、递归删除磁盘上的指定文件夹目录及文件
|
||
* 4、递归获取磁盘上的指定目录下所有文件的集合,返回类型是:字典[文件名,要压缩的相对文件名]
|
||
* 5、解压Zip文件,并覆盖保存到指定的目标路径文件夹下
|
||
* 6、获取Zip压缩包中的文件列表
|
||
* Date:2021
|
||
* Version:0.1版本
|
||
* Author:Coffee
|
||
* Modify Recoder:
|
||
*/
|
||
|
||
//public class Program
|
||
//{
|
||
// private static string directoryPath = @".\temp";
|
||
// public static void Main()
|
||
// {
|
||
// DirectoryInfo directorySelected = new DirectoryInfo(directoryPath);
|
||
// Compress(directorySelected);
|
||
|
||
// foreach (FileInfo fileToDecompress in directorySelected.GetFiles("*.gz"))
|
||
// {
|
||
// Decompress(fileToDecompress);
|
||
// }
|
||
// }
|
||
|
||
// public static void Compress(DirectoryInfo directorySelected)
|
||
// {
|
||
// foreach (FileInfo fileToCompress in directorySelected.GetFiles())
|
||
// {
|
||
// using (FileStream originalFileStream = fileToCompress.OpenRead())
|
||
// {
|
||
// if ((File.GetAttributes(fileToCompress.FullName) &
|
||
// FileAttributes.Hidden) != FileAttributes.Hidden & fileToCompress.Extension != ".gz")
|
||
// {
|
||
// using (FileStream compressedFileStream = File.Create(fileToCompress.FullName + ".gz"))
|
||
// {
|
||
// using (GZipStream compressionStream = new GZipStream(compressedFileStream,
|
||
// CompressionMode.Compress))
|
||
// {
|
||
// originalFileStream.CopyTo(compressionStream);
|
||
// }
|
||
// }
|
||
// FileInfo info = new FileInfo(directoryPath + Path.DirectorySeparatorChar + fileToCompress.Name + ".gz");
|
||
// Debug.Log($"Compressed {fileToCompress.Name} from {fileToCompress.Length.ToString()} to {info.Length.ToString()} bytes.");
|
||
// }
|
||
// }
|
||
// }
|
||
// }
|
||
|
||
// public static void Decompress(FileInfo fileToDecompress)
|
||
// {
|
||
// using (FileStream originalFileStream = fileToDecompress.OpenRead())
|
||
// {
|
||
// string currentFileName = fileToDecompress.FullName;
|
||
// string newFileName = currentFileName.Remove(currentFileName.Length - fileToDecompress.Extension.Length);
|
||
|
||
// using (FileStream decompressedFileStream = File.Create(newFileName))
|
||
// {
|
||
// using (GZipStream decompressionStream = new GZipStream(originalFileStream, CompressionMode.Decompress))
|
||
// {
|
||
// decompressionStream.CopyTo(decompressedFileStream);
|
||
// Debug.Log($"Decompressed: {fileToDecompress.Name}");
|
||
// }
|
||
// }
|
||
// }
|
||
// }
|
||
//} |