.net 筆記

學習.net
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

生成 zip文件

Posted on 2007-10-04 10:31  陳偉  阅读(276)  评论(0)    收藏  举报

 private void generate_zip()
        {
            string[] FileProperties = new string[2];
            FileProperties[0] = directory + "\\" + foldername + "\\" + DateTime.Now.ToString("yyyyMMdd");
            FileProperties[1] = directory + "\\" + foldername + "\\" + DateTime.Now.ToString("yyyyMMdd") +"\\"+zipfilename;
            DirectoryInfo dir1 = new DirectoryInfo(FileProperties[0].ToString());
            int m = dir1.GetFiles("*.csv").Length;
            int n = dir1.GetFiles("*.xls").Length;

            //int m = dir1.GetFiles().Length;
            if (m > 0||n>0)
            {
                try
                {
                    ZipClass Zc = new ZipClass();
                    Zc.ZipFileMain(FileProperties);

                    foreach (FileInfo f1 in dir1.GetFiles("*.csv"))
                    {

                        //FileStream fs = new FileStream(f1.FullName, FileMode.CreateNew);
                        //fs.Close();
                        f1.Delete();//
                    }

                    foreach (FileInfo f1 in dir1.GetFiles("*.xls"))
                    {
                        f1.Delete();//
                    }
                }
                catch (Exception ee)
                {
                    error_update(jobid);
                    WriteLog(ee.Message);
                    Send_Mail("Shengming_Lee@compal.com", "APS_CIC_IT@compal.com", "系統Re-model Report", ee.Message, "");
                    return;
                }
            }

}

 public void ZipFile(string FileToZip, string ZipedFile, int CompressionLevel, int BlockSize)
        {

            if (!System.IO.File.Exists(FileToZip))
            {
                throw new System.IO.FileNotFoundException("The specified file " + FileToZip + " could not be found. Zipping aborderd");
            }

            System.IO.FileStream StreamToZip = new System.IO.FileStream(FileToZip, System.IO.FileMode.Open, System.IO.FileAccess.Read);
            System.IO.FileStream ZipFile = System.IO.File.Create(ZipedFile);
            ZipOutputStream ZipStream = new ZipOutputStream(ZipFile);
            ZipEntry ZipEntry = new ZipEntry("ZippedFile");
            ZipStream.PutNextEntry(ZipEntry);
            ZipStream.SetLevel(CompressionLevel);
            byte[] buffer = new byte[BlockSize];
            System.Int32 size = StreamToZip.Read(buffer, 0, buffer.Length);
            ZipStream.Write(buffer, 0, size);
            try
            {
                while (size < StreamToZip.Length)
                {
                    int sizeRead = StreamToZip.Read(buffer, 0, buffer.Length);
                    ZipStream.Write(buffer, 0, sizeRead);
                    size += sizeRead;
                }
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            ZipStream.Finish();
            ZipStream.Close();
            StreamToZip.Close();
        }

        public void ZipFileMain(string[] args)
        {
            DirectoryInfo dir1 = new DirectoryInfo(args[0].ToString());
            int m = dir1.GetFiles("*.csv").Length;
            int n = dir1.GetFiles("*.xls").Length;

            string[] filenames = new string[250];
            //string[] filenames=Directory.GetFiles(args[0], "*.csv")
            if(m > 0)
            {
                 filenames = Directory.GetFiles(args[0], "*.csv");
            }
            if (n > 0)
            {
                 filenames = Directory.GetFiles(args[0], "*.xls");
            }
           
            Crc32 crc = new Crc32();
            ZipOutputStream s = new ZipOutputStream(File.Create(args[1]));

            s.SetLevel(6); // 0 - store only to 9 - means best compression

            foreach (string file in filenames)
            {

                FileStream fs = File.OpenRead(file);

                byte[] buffer = new byte[fs.Length];
                fs.Read(buffer, 0, buffer.Length);
                ZipEntry entry = new ZipEntry(file.Remove(0, args[0].Length));

                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);

            }

            s.Finish();
            s.Close();
        }