123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Threading.Tasks;
- using ICSharpCode.SharpZipLib.Checksum;
- using ICSharpCode.SharpZipLib.Zip;
- namespace Team.Utility
- {
- public class ZipUtility
- {
-
-
-
-
-
-
-
-
-
-
- public static void Zip(string fileToZip, string zipPath, string zippedFileName = "",
- int compressionLevel = 5, int blockSize = 2048, bool isEncrypt = true)
- {
-
- if (!File.Exists(fileToZip))
- {
- throw new FileNotFoundException("指定要压缩的文件: " + fileToZip + " 不存在!");
- }
-
- var zipFileName = string.IsNullOrEmpty(zippedFileName)
- ? zipPath + "\\" +
- new FileInfo(fileToZip).Name.Substring(0, new FileInfo(fileToZip).Name.LastIndexOf('.')) + ".zip"
- : zipPath + "\\" + zippedFileName + ".zip";
- using (var zipFile = File.Create(zipFileName))
- {
- using (var zipStream = new ZipOutputStream(zipFile))
- {
- using (var streamToZip = new FileStream(fileToZip, FileMode.Open, FileAccess.Read))
- {
- var fileName = fileToZip.Substring(fileToZip.LastIndexOf("\\", StringComparison.Ordinal) + 1);
- var zipEntry = new ZipEntry(fileName);
- if (isEncrypt)
- {
-
- zipStream.Password = "123";
- }
- zipStream.PutNextEntry(zipEntry);
-
- zipStream.SetLevel(compressionLevel);
-
- var buffer = new byte[blockSize];
- int sizeRead;
- do
- {
- sizeRead = streamToZip.Read(buffer, 0, buffer.Length);
- zipStream.Write(buffer, 0, sizeRead);
- } while (sizeRead > 0);
- streamToZip.Close();
- }
- zipStream.Finish();
- zipStream.Close();
- }
- zipFile.Close();
- }
- }
-
-
-
-
-
-
-
-
- public static void ZipDirectory(string directoryToZip, string zipedPath, string zipedFileName = "",
- bool isEncrypt = true)
- {
-
- if (!Directory.Exists(directoryToZip))
- {
- throw new FileNotFoundException("指定的目录: " + directoryToZip + " 不存在!");
- }
-
- var zipFileName = string.IsNullOrEmpty(zipedFileName)
- ? zipedPath + "\\" + new DirectoryInfo(directoryToZip).Name + ".zip"
- : zipedPath + "\\" + zipedFileName + ".zip";
- using (var zipFile = File.Create(zipFileName))
- {
- using (var s = new ZipOutputStream(zipFile))
- {
- if (isEncrypt)
- {
-
- s.Password = "123";
- }
- ZipSetp(directoryToZip, s, "");
- }
- }
- }
-
-
-
-
-
-
-
- public static async Task ZipDirectoryAsync(string directoryToZip, string zipedPath, string zipedFileName = "",
- bool isEncrypt = true)
- {
-
- if (!Directory.Exists(directoryToZip))
- {
- throw new FileNotFoundException("指定的目录: " + directoryToZip + " 不存在!");
- }
-
- var zipFileName = string.IsNullOrEmpty(zipedFileName)
- ? zipedPath + "\\" + new DirectoryInfo(directoryToZip).Name + ".zip"
- : zipedPath + "\\" + zipedFileName + ".zip";
- using var zipFile = File.Create(zipFileName);
- using var s = new ZipOutputStream(zipFile);
- if (isEncrypt)
- {
-
- s.Password = "123";
- }
- await ZipSetpAsync(directoryToZip, s, "");
- }
-
-
-
-
-
- public static void ZipFileDirectory(string strDirectory, string zipedFile)
- {
- using (System.IO.FileStream ZipFile = System.IO.File.Create(zipedFile))
- {
- using (ZipOutputStream s = new ZipOutputStream(ZipFile))
- {
- ZipSetp(strDirectory, s, "");
- }
- }
- }
- public static void ZipFiles(List<string> filesPath,string zipedPath,string zipedFileName = "",
- bool isEncrypt = true)
- {
-
- foreach (var filePath in filesPath)
- {
- throw new FileNotFoundException("指定的目录: " + filePath + " 不存在!");
- }
-
- var zipFileName = string.IsNullOrEmpty(zipedFileName)
- ? zipedPath + "\\" + new DirectoryInfo(filesPath.First()).Name + ".zip"
- : zipedPath + "\\" + zipedFileName + ".zip";
- using (var zipFile = File.Create(zipFileName))
- {
- using (var s = new ZipOutputStream(zipFile))
- {
- if (isEncrypt)
- {
-
- s.Password = "123";
- }
- ZipSetp(filesPath.First(), s, "");
- }
- }
- }
- public static async Task ZipFilesAsync(List<string> filesPathes, string zipedPath, string zipedFileName = "",
- int compressionLevel = 5, int blockSize = 2048, bool isEncrypt = true)
- {
- foreach (var filesPath in filesPathes)
- {
-
- if (!File.Exists(filesPath))
- {
- throw new FileNotFoundException("指定要压缩的文件: " + filesPath + " 不存在!");
- }
- }
-
-
- var zipFileName = string.IsNullOrEmpty(zipedFileName)
- ? zipedPath + "\\" +
- new FileInfo(zipedPath).Name.Substring(0, new FileInfo(zipedPath).Name.LastIndexOf('.')) + ".zip"
- : zipedPath + "\\" + zipedFileName + ".zip";
- using (var zipFile = File.Create(zipFileName))
- {
- using (var zipStream = new ZipOutputStream(zipFile))
- {
- foreach (var filesPath in filesPathes)
- {
- using (var streamToZip = new FileStream(filesPath, FileMode.Open, FileAccess.Read))
- {
- var fileName =
- filesPath.Substring(filesPath.LastIndexOf("\\", StringComparison.Ordinal) + 1);
- var zipEntry = new ZipEntry(fileName);
- if (isEncrypt)
- {
-
- zipStream.Password = "123";
- }
- zipStream.PutNextEntry(zipEntry);
-
- zipStream.SetLevel(compressionLevel);
-
- var buffer = new byte[blockSize];
- int sizeRead;
- do
- {
- sizeRead = await streamToZip.ReadAsync(buffer, 0, buffer.Length);
- await zipStream.WriteAsync(buffer, 0, sizeRead);
- } while (sizeRead > 0);
- }
- }
-
- }
- }
- }
-
-
-
-
- private static void ZipSetp(string strDirectory, ZipOutputStream s, string parentPath)
- {
- if (strDirectory[strDirectory.Length - 1] != Path.DirectorySeparatorChar)
- {
- strDirectory += Path.DirectorySeparatorChar;
- }
- var crc = new Crc32();
- var fileNames = Directory.GetFileSystemEntries(strDirectory);
- foreach (var file in fileNames)
- {
- if (Directory.Exists(file))
- {
- var pPath = parentPath;
- pPath += file.Substring(file.LastIndexOf("\\", StringComparison.Ordinal) + 1);
- pPath += "\\";
- ZipSetp(file, s, pPath);
- }
- else
- {
-
- using (var fs = File.OpenRead(file))
- {
- var buffer = new byte[fs.Length];
- fs.Read(buffer, 0, buffer.Length);
- var fileName = parentPath +
- file.Substring(file.LastIndexOf("\\", StringComparison.Ordinal) + 1);
- var entry = new ZipEntry(fileName);
- entry.DateTime = DateTime.Now;
- entry.Size = fs.Length;
- fs.Close();
- crc.Reset();
- crc.Update(buffer);
- entry.Crc = crc.Value;
- s.PutNextEntry(entry);
- s.Write(buffer, 0, buffer.Length);
- }
- }
- }
- }
-
-
- private static async Task ZipSetpAsync(string strDirectory, ZipOutputStream s, string parentPath)
- {
- if (strDirectory[strDirectory.Length - 1] != Path.DirectorySeparatorChar)
- {
- strDirectory += Path.DirectorySeparatorChar;
- }
- var crc = new Crc32();
- var fileNames = Directory.GetFileSystemEntries(strDirectory);
- foreach (var file in fileNames)
- {
- if (Directory.Exists(file))
- {
- var pPath = parentPath;
- pPath += file.Substring(file.LastIndexOf("\\", StringComparison.Ordinal) + 1);
- pPath += "\\";
- ZipSetp(file, s, pPath);
- }
- else
- {
-
- using var fs = File.OpenRead(file);
- var buffer = new byte[fs.Length];
- await fs.ReadAsync(buffer, 0, buffer.Length);
- var fileName = parentPath +
- file.Substring(file.LastIndexOf("\\", StringComparison.Ordinal) + 1);
- var entry = new ZipEntry(fileName);
- entry.DateTime = DateTime.Now;
- entry.Size = fs.Length;
- crc.Reset();
- crc.Update(buffer);
- entry.Crc = crc.Value;
- s.PutNextEntry(entry);
- await s.WriteAsync(buffer, 0, buffer.Length);
- }
- }
- }
-
-
-
-
-
-
-
-
- public static void UnZip(string zipFile, string targetDirectory, string password, bool overWrite = true)
- {
-
- if (!Directory.Exists(targetDirectory))
- {
- throw new FileNotFoundException("指定的目录: " + targetDirectory + " 不存在!");
- }
-
- if (!targetDirectory.EndsWith("\\")) { targetDirectory = targetDirectory + "\\"; }
- using (var zipFiles = new ZipInputStream(File.OpenRead(zipFile)))
- {
- zipFiles.Password = password;
- ZipEntry theEntry;
- while ((theEntry = zipFiles.GetNextEntry()) != null)
- {
- var directoryName = "";
- var pathToZip = theEntry.Name;
- if (pathToZip != "")
- directoryName = Path.GetDirectoryName(pathToZip) + "\\";
- var fileName = Path.GetFileName(pathToZip);
- Directory.CreateDirectory(targetDirectory + directoryName);
- if (fileName == "") continue;
- if ((!File.Exists(targetDirectory + directoryName + fileName) || !overWrite) &&
- (File.Exists(targetDirectory + directoryName + fileName))) continue;
- using (var streamWriter = File.Create(targetDirectory + directoryName + fileName))
- {
- var data = new byte[2048];
- while (true)
- {
- var size = zipFiles.Read(data, 0, data.Length);
- if (size > 0)
- streamWriter.Write(data, 0, size);
- else
- break;
- }
- streamWriter.Close();
- }
- }
- zipFiles.Close();
- }
- }
- }
- }
|