未处理的“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();
}
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();
}
解决方案 »
- C#如何防止文件夹被删除?
- winform中调用webservice 的配置问题。请教了,在线等
- Textbox数据绑定问题
- C#调用infoplus21_api.dll问题
- 请教,一个关于时间的问题
- Administrator帐户密码被改问题,望高手进……
- 为什么StreamReader读来的汉字都是乱码
- 有关用C#控制FLASH的显示问题,查了92条帖子,没有我想要的,只好再问了,谢谢先
- 请教关于用C#来写与数据库的思想!
- 初级的数据库问题,但是没有发现一本书提到,哪位大哥帮一下忙?
- 我在vs2005中做窗口的继承系统出现如下错误,但是在vs2003就没有问题。请大侠指点
- 关于系统事件的调用
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(); }