/// <summary> 压缩数据 </summary>
        public static byte[] Compress(byte[] source)
        {
#if false//SCORPIO_UWP && !UNITY_EDITOR
            using (MemoryStream stream = new MemoryStream()) {
                System.IO.Compression.ZipArchive      zipStream = new System.IO.Compression.ZipArchive(stream, System.IO.Compression.ZipArchiveMode.Create);
                System.IO.Compression.ZipArchiveEntry zipEntry  = zipStream.CreateEntry("0.txt");
                Stream entryStream = zipEntry.Open();
                entryStream.Write(source, 0, source.Length);
                entryStream.Flush();
                entryStream.Dispose();
                zipStream.Dispose();
                byte[] ret = stream.ToArray();
                stream.Dispose();
                return(ret);
            }
#else
            using (MemoryStream stream = new MemoryStream()) {
                ICSharpCode.SharpZipLib.Zip.ZipOutputStream zipStream = new ICSharpCode.SharpZipLib.Zip.ZipOutputStream(stream);
                zipStream.PutNextEntry(new ICSharpCode.SharpZipLib.Zip.ZipEntry("0.txt"));
                zipStream.Write(source, 0, source.Length);
                zipStream.Finish();
                byte[] ret = stream.ToArray();
                zipStream.Dispose();
                stream.Dispose();
                return(ret);
            }
#endif
        }
Beispiel #2
0
 protected virtual void Dispose(bool disposing)
 {
     if (disposing)
     {
         if (zipStream != null)
         {
             zipStream.Finish();
             zipStream.Dispose();
         }
     }
 }
Beispiel #3
0
        /// <summary>
        /// 压缩文件,只支持zip压缩
        /// </summary>
        /// <param name="fileAbsolutePathList">文件绝对路径的列表</param>
        /// <param name="compressFileName">压缩后文件的名称</param>
        /// <param name="isCoverOrNew">true为覆盖,false为新建</param>
        /// <returns></returns>
        public static string CompressFiles(List <string> fileAbsolutePathList, List <string> fileNameList, string compressFileName, bool isCoverOrNew)
        {
            if (fileAbsolutePathList == null || fileAbsolutePathList.Count < 1)
            {
                throw new Exception("至少传入一个文件或文件夹的绝对路径!");
            }
            string firstPath = fileAbsolutePathList[0].Replace('/', '\\');
            string dirPath   = firstPath.Substring(0, firstPath.LastIndexOf('\\') + 1);

            if (string.IsNullOrEmpty(compressFileName))
            {
                compressFileName = firstPath.Substring(firstPath.LastIndexOf('\\'));//取出最后一个\后面的字符串 作为文件名
                if (System.IO.File.Exists(firstPath))
                {
                    compressFileName = compressFileName.Substring(0, compressFileName.LastIndexOf('.'));//去除文件名的扩展名
                }
            }
            if (!Path.IsPathRooted(compressFileName))
            {
                compressFileName = GetNewFilePath(dirPath, compressFileName + ".zip", isCoverOrNew);                                                                    //获取合法的文件名
            }
            ICSharpCode.SharpZipLib.Zip.ZipOutputStream zipOutput = new ICSharpCode.SharpZipLib.Zip.ZipOutputStream(new FileStream(compressFileName, FileMode.Create)); //新建压缩文件流 “ZipOutputStream”
            try
            {
                zipOutput.SetLevel(9); //压缩等级
                for (int i = 0; i < fileAbsolutePathList.Count; i++)
                {
                    if (fileNameList == null || fileNameList.Count != fileAbsolutePathList.Count)
                    {
                        appendStream(zipOutput, fileAbsolutePathList[i], dirPath);
                    }
                    else
                    {
                        appendStream(zipOutput, fileAbsolutePathList[i], fileNameList[i], dirPath);
                    }
                }
                zipOutput.Finish();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                zipOutput.Close();
                zipOutput.Dispose();
            }

            return(compressFileName);
        }