/// <summary> /// Zip压缩至byte[] /// </summary> /// <param name="srcFolder"></param> /// <param name="最顶层是否是文件夹(如果不是,则是文件夹下的一个个文件)"></param> /// <returns></returns> public static byte[] CompressFromFolder(string srcFolder, bool topFolder) { AbstractFile memoryFile = new MemoryFile(); //AbstractFile memoryFile = new DiskFile(System.IO.Path.GetTempFileName()); ZipArchive archive = CreateZipArchive(memoryFile); if (topFolder) { AbstractFolder source = new DiskFolder(srcFolder); source.CopyTo(archive, true); } else { foreach (var i in archive.GetItems(false)) { i.CopyTo(archive, true); } } byte[] buffer; using (Stream destStream = memoryFile.OpenRead(FileShare.Read)) { buffer = new byte[destStream.Length]; destStream.Read(buffer, 0, buffer.Length); destStream.Close(); } memoryFile.Delete(); return(buffer); }
public Stream OpenRead() => filedata.OpenRead();
/// <summary> /// Zip压缩至byte[] /// </summary> /// <param name="srcFolder"></param> /// <param name="最顶层是否是文件夹(如果不是,则是文件夹下的一个个文件)"></param> /// <returns></returns> public static byte[] CompressFromFolder(string srcFolder, bool topFolder) { AbstractFile memoryFile = new MemoryFile(); //AbstractFile memoryFile = new DiskFile(System.IO.Path.GetTempFileName()); ZipArchive archive = CreateZipArchive(memoryFile); if (topFolder) { AbstractFolder source = new DiskFolder(srcFolder); source.CopyTo(archive, true); } else { foreach (var i in archive.GetItems(false)) { i.CopyTo(archive, true); } } byte[] buffer; using (Stream destStream = memoryFile.OpenRead(FileShare.Read)) { buffer = new byte[destStream.Length]; destStream.Read(buffer, 0, buffer.Length); destStream.Close(); } memoryFile.Delete(); return buffer; }