未处理的“System.IO.IOException”类型的异常出现在 mscorlib.dll 中。其他信息: 该进程无法访问文件“f:\s.txt”,因为该文件正由另一进程使用。代码:
else if(File.Exists(s))  //文件的处理
{
u.SetLevel(9);      //压缩等级
FileStream f = File.OpenRead(s);

//FileStream f=new FileStream(s,FileMode.Open,FileAccess.Read);
byte[] b = new byte[f.Length];
f.Read(b,0,b.Length);          //将文件流加入缓冲字节中
ZipEntry z = new ZipEntry(this.ShortDir(s));
u.PutNextEntry(z);             //为压缩文件流提供一个容器
u.Write(b,0,b.Length);  //写入字节
f.Close();
}

解决方案 »

  1.   

    请检查f:\s.txt,确认没有其他程序在使用,或者是生成了之后的确关闭了文件。
      

  2.   

    請看下面的代碼
    private void ZipFile(string sourceFilePath,string ZipFilePath)
    { Crc32 crc = new Crc32();
    ZipOutputStream s = new ZipOutputStream(File.Create(ZipFilePath));

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

    FileStream fs = File.OpenRead(sourceFilePath);

    byte[] buffer = new byte[fs.Length];
    fs.Read(buffer, 0, buffer.Length);
    ZipEntry entry = new ZipEntry(sourceFilePath.Substring(sourceFilePath.LastIndexOf(@"\")+1));
    entry.DateTime = DateTime.Now;

    // set Size and the crc, because the information
    // about the size and crc should be stored in the header
    // if it is not set it is automatically written in the footer.
    // (in this case size == crc == -1 in the header)
    // Some ZIP programs have problems with zip files that don't store
    // the size and crc in the header.
    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();
    } private void ZipDirectory(string sourceDirectory,string zipFilePath)
    {
    string[] filenames = Directory.GetFiles(sourceDirectory);

    Crc32 crc = new Crc32();
    ZipOutputStream s = new ZipOutputStream(File.Create(zipFilePath));

    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.Substring(file.LastIndexOf(@"\")+1));
    entry.DateTime = DateTime.Now;

    // set Size and the crc, because the information
    // about the size and crc should be stored in the header
    // if it is not set it is automatically written in the footer.
    // (in this case size == crc == -1 in the header)
    // Some ZIP programs have problems with zip files that don't store
    // the size and crc in the header.
    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(); }
      

  3.   

    請比較一下我上面的代碼可能是你的Finish()方法沒有調用