增加上传华为云obs接口
This commit is contained in:
145
20220330_Vote/Vote.Services/Tools/ZipHelper.cs
Normal file
145
20220330_Vote/Vote.Services/Tools/ZipHelper.cs
Normal file
@@ -0,0 +1,145 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using ICSharpCode.SharpZipLib.Checksums;
|
||||
using ICSharpCode.SharpZipLib.Zip;
|
||||
|
||||
namespace Vote.Services.Tools
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public class ZipHelper
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// 所有文件缓存
|
||||
/// </summary>
|
||||
List<string> files = new List<string>();
|
||||
/// <summary>
|
||||
/// 所有空目录缓存
|
||||
/// </summary>
|
||||
List<string> paths = new List<string>();
|
||||
/// <summary>
|
||||
/// 取得目录下所有文件及文件夹,分别存入files及paths
|
||||
/// </summary>
|
||||
/// <param name="rootPath">根目录</param>
|
||||
private void GetAllDirectories(string rootPath)
|
||||
{
|
||||
string[] subPaths = Directory.GetDirectories(rootPath);//得到所有子目录
|
||||
foreach (string path in subPaths)
|
||||
{
|
||||
GetAllDirectories(path);//对每一个字目录做与根目录相同的操作:即找到子目录并将当前目录的文件名存入List
|
||||
}
|
||||
string[] files = Directory.GetFiles(rootPath);
|
||||
foreach (string file in files)
|
||||
{
|
||||
this.files.Add(file);//将当前目录中的所有文件全名存入文件List
|
||||
}
|
||||
if (subPaths.Length == files.Length && files.Length == 0)//如果是空目录
|
||||
{
|
||||
this.paths.Add(rootPath);//记录空目录
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 压缩目录(包括子目录及所有文件)
|
||||
/// </summary>
|
||||
/// <param name="rootPath">要压缩的根目录</param>
|
||||
/// <param name="destinationPath">保存路径</param>
|
||||
/// <param name="compressLevel">压缩程度,范围0-9,数值越大,压缩程序越高</param>
|
||||
public void ZipFileFromDirectory(string rootPath, string destinationPath, int compressLevel)
|
||||
{
|
||||
this.files.Clear();
|
||||
GetAllDirectories(rootPath);
|
||||
|
||||
string rootMark = rootPath + "\\";//得到当前路径的位置,以备压缩时将所压缩内容转变成相对路径。
|
||||
Crc32 crc = new Crc32();
|
||||
using ZipOutputStream outPutStream = new ZipOutputStream(System.IO.File.Create(destinationPath));
|
||||
outPutStream.SetLevel(compressLevel); // 0 - store only to 9 - means best compression
|
||||
foreach (string file in files)
|
||||
{
|
||||
using FileStream fileStream = System.IO.File.OpenRead(file);//打开压缩文件
|
||||
byte[] buffer = new byte[fileStream.Length];
|
||||
fileStream.Read(buffer, 0, buffer.Length);
|
||||
ZipEntry entry = new ZipEntry(file.Replace(rootMark, string.Empty));
|
||||
entry.DateTime = DateTime.Now;
|
||||
|
||||
entry.Size = fileStream.Length;
|
||||
fileStream.Close();
|
||||
crc.Reset();
|
||||
crc.Update(buffer);
|
||||
entry.Crc = crc.Value;
|
||||
outPutStream.PutNextEntry(entry);
|
||||
outPutStream.Write(buffer, 0, buffer.Length);
|
||||
}
|
||||
|
||||
this.files.Clear();
|
||||
|
||||
foreach (string emptyPath in paths)
|
||||
{
|
||||
ZipEntry entry = new ZipEntry(emptyPath.Replace(rootMark, string.Empty) + "/");
|
||||
outPutStream.PutNextEntry(entry);
|
||||
}
|
||||
|
||||
this.paths.Clear();
|
||||
outPutStream.Finish();
|
||||
outPutStream.Close();
|
||||
GC.Collect();
|
||||
}
|
||||
/// <summary>
|
||||
/// 压缩单个文件
|
||||
/// <param name="filepath">要压缩的文件路径</param>
|
||||
/// <param name="compressLevel">压缩程度,范围0-9,数值越大,压缩程序越高</param>
|
||||
/// <paramref name="dwgZipPath">输出zip文件路径</paramref>
|
||||
/// </summary>
|
||||
public bool ZipOneFile(string filepath, int compressLevel)
|
||||
{
|
||||
var r = false;
|
||||
try
|
||||
{
|
||||
var file = new FileInfo(filepath);
|
||||
var dwgZipPath = GetZipFileName(filepath);
|
||||
Crc32 crc = new Crc32();
|
||||
using ZipOutputStream outPutStream = new ZipOutputStream(System.IO.File.Create(dwgZipPath));
|
||||
outPutStream.SetLevel(compressLevel); // 0 - store only to 9 - means best compression
|
||||
using FileStream fileStream = System.IO.File.OpenRead(filepath);//打开压缩文件
|
||||
byte[] buffer = new byte[fileStream.Length];
|
||||
fileStream.Read(buffer, 0, buffer.Length);
|
||||
ZipEntry entry = new ZipEntry(filepath.Replace(file.DirectoryName + "\\", string.Empty));
|
||||
entry.DateTime = DateTime.Now;
|
||||
entry.Size = fileStream.Length;
|
||||
fileStream.Close();
|
||||
crc.Reset();
|
||||
crc.Update(buffer);
|
||||
entry.Crc = crc.Value;
|
||||
outPutStream.PutNextEntry(entry);
|
||||
outPutStream.Write(buffer, 0, buffer.Length);
|
||||
this.files.Clear();
|
||||
this.paths.Clear();
|
||||
outPutStream.Finish();
|
||||
outPutStream.Close();
|
||||
GC.Collect();
|
||||
r = true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
r = false;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
private string GetZipFileName(string filepath)
|
||||
{
|
||||
//if (System.IO.File.Exists(filepath + ".zip"))
|
||||
//{
|
||||
// return GetZipFileName(filepath + ".zip");
|
||||
//}
|
||||
return filepath + ".zip";
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user