想要压缩的是outlook express保存邮件的文件夹
但是如果outlook express在运行中的话,就会显示文件无法访问
请问怎么才能解决呢?
贴下相关代码,明明只是用OpenRead打开,为什么会无法访问呢?
尝试了下把txt文件在编辑状态的情况下打包,却不会出现问题,为什么呢=v= static ZipOutputStream zos = null;
        static String strBaseDir = "";        public static int ZipFile(string foldDir, string zipPath)
        {
            string[] filenames;            try
            {
                filenames = Directory.GetFiles(foldDir);
            }
            catch
            {
                MessageBox.Show("err1");
                return 1;
            }
            try
            {
                zos = new ZipOutputStream(File.Create(zipPath));
            }
            catch
            {
                MessageBox.Show("err2");
                return 2;
            }            zos.SetLevel(9); // 0 - store only to 9 - means best compression
            strBaseDir = foldDir + @"\";
            addZipEntry(foldDir);            zos.Finish();
            zos.Close();
            return 0;
        }        public static void addZipEntry(string PathStr)
        {
            DirectoryInfo di = new DirectoryInfo(PathStr);
            foreach (DirectoryInfo item in di.GetDirectories())//all fold   
            {
                addZipEntry(item.FullName);
            }
            foreach (FileInfo item in di.GetFiles())//all file  
            {
                FileStream fs;
                try
                {
                    fs = File.OpenRead(item.FullName);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                    continue;
                }                byte[] buffer = new byte[fs.Length];
                fs.Read(buffer, 0, buffer.Length);
                string strEntryName = item.FullName.Replace(strBaseDir, "");//dir
                ZipEntry entry = new ZipEntry(strEntryName);
                zos.PutNextEntry(entry);
                zos.Write(buffer, 0, buffer.Length);
                fs.Close();
            }
        }

解决方案 »

  1.   

      public void ZipFold()
            {
                //获得压缩的文件夹
                //压缩文件夹的名字
                string name = file + ".rar";
                //压缩文件的流对象
                // MessageBox.Show(name);
                ZipOutputStream output = new ZipOutputStream(File.Create(name));
                output.SetLevel(6);
                string[] dir = Directory.GetFiles(file);
                //存放文件数据
                 Crc32 crc = new Crc32();
                foreach (string myFile in dir)
                {
                    FileStream fs = new FileStream(myFile, FileMode.Open, FileAccess.Read);
                    byte[] bt = new byte[fs.Length];
                    fs.Read(bt, 0, bt.Length);
                    //存储要压缩的文件
                    ZipEntry entry = new ZipEntry(myFile.Substring(myFile.Length - 41, 41));
                    entry.Size = fs.Length;
                    entry.DateTime = DateTime.Now;
                    fs.Close();
                    crc.Reset();  //清除crc内容
                    crc.Update(bt);  //更新文件内容到crc中
                    entry.Crc = crc.Value;   //将文件内容放到压缩文件中
                    output.PutNextEntry(entry);
                    //将数据写入压缩流中
                    output.Write(bt, 0, bt.Length);
                }
                output.Close();
                Console.WriteLine("压缩成功!");
            }
      

  2.   

    谢谢楼上,不过下面这句的时候,还是一样报错说无法访问
    FileStream fs = new FileStream(myFile, FileMode.Open, FileAccess.Read);
    抹泪ing