C#文件压缩

写数据

public static void FileZip()
        {
            string filePath = "D:\\测试";
            string fileName = filePath + "\\test.txt";
            if (!Directory.Exists(filePath))
            {
                Directory.CreateDirectory(filePath);
            }
            else 
            {
                if (File.Exists(fileName))
                {
                    System.IO.DirectoryInfo fileInfo = new DirectoryInfo(filePath);
                    fileInfo.Attributes = FileAttributes.Normal & FileAttributes.Directory;
                    //去除文件的只读属性
                    System.IO.File.SetAttributes(filePath, System.IO.FileAttributes.Normal);
                    File.Delete(filePath);
                }
                if (File.Exists(filePath + "\\测试.zip"))
                {
                    File.Delete(filePath + "\\测试.zip");
                }
                Directory.Delete(filePath);
                Directory.CreateDirectory(filePath);
            }
            
            Encoding end = new UTF8Encoding(false);
            StreamWriter json_Data = new StreamWriter(fileName, false, end);
            for (int i = 0; i < 10; i++)
            {

                string aa = "测试" + i + "\t" + "测试" + i + "\t" + "测试" + i + "\t" +
                      "测试" + i + "\t" + "测试" + i + "\t" + "测试" + i + "\t" +
                      "测试" + i;


                json_Data.WriteLine(aa);
                json_Data.Flush();
            }
            json_Data.Close();
            json_Data.Dispose();
            #region 压缩
            //压缩
            CreateZipFile(fileName, filePath + "\\测试.zip");
            FileStream File1 = new FileStream(filePath + "\\测试.zip", FileMode.Open, FileAccess.Read);//打开文件
            byte[] MoldSetData = new byte[File1.Length];
            File1.Read(MoldSetData, 0, MoldSetData.Length);
            File1.Close();

            sbyte[] mySByte = new sbyte[MoldSetData.Length];

            for (int i = 0; i < MoldSetData.Length; i++)
            {
                if (MoldSetData[i] > 127)
                    mySByte[i] = (sbyte)(MoldSetData[i] - 256);
                else
                    mySByte[i] = (sbyte)MoldSetData[i];
            }
            #endregion 

        }

压缩文件

 private static void CreateZipFile(string filesPath, string zipFilePath)
        {
            try
            {
                using (ZipOutputStream s = new ZipOutputStream(File.Create(zipFilePath)))
                {

                    s.SetLevel(9); // 压缩级别 0-9
                    s.Password = "123"; //Zip压缩文件密码
                    byte[] buffer = new byte[4096]; //缓冲区大小
                    ZipEntry entry = new ZipEntry(System.IO.Path.GetFileName(filesPath));
                    entry.DateTime = DateTime.Now;
                    s.PutNextEntry(entry);
                    FileStream fs = new FileStream(filesPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                    StreamReader sr = new StreamReader(fs, System.Text.Encoding.Default);

                    using (sr)
                    {
                        int sourceBytes;
                        do
                        {
                            sourceBytes = fs.Read(buffer, 0, buffer.Length);
                            s.Write(buffer, 0, sourceBytes);
                        } while (sourceBytes > 0);
                    }
                    s.Finish();
                    s.Close();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("处理过程中出现异常", ex);
            }

 

posted on 2024-02-05 10:26  棽犹  阅读(33)  评论(0编辑  收藏  举报

导航