![](/img/trans.png)
[英]How to give an Order to the files included in zip file created with dotnet Zip
[英]What would cause files being added to a zip file to not be included in the zip?
我使用的程序需要处理大量数据,然后将数据转换为xml文件,然后获取这些xml文件并将其压缩以供其他程序使用。 有时,在压缩过程中,一个或两个xml文件被忽略了。 这是相当罕见的,每月一次或两次,但是一旦发生,那就太麻烦了。 我正在寻找帮助,以弄清为什么文件未压缩以及如何防止压缩。 这段代码很简单:
public string AddToZip(string outfile, string toCompress)
{
if (!File.Exists(toCompress)) throw new FileNotFoundException("Could not find the file to compress", toCompress);
string dir = Path.GetDirectoryName(outfile);
if(!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
// The program that gets this data can't handle files over
// 20 MB, so it splits it up into two or more files if it hits the
// limit.
if (File.Exists(outfile))
{
FileInfo tooBig = new FileInfo(outfile);
int converter = 1024;
float fileSize = tooBig.Length / converter; //bytes to KB
fileSize = fileSize / converter; //KB to MB
int limit = CommonTypes.Helpers.ConfigHelper.GetConfigEntryInt("zipLimit", "19");
if (fileSize >= limit)
{
outfile = MakeNewName(outfile);
}
}
using (ZipFile zf = new ZipFile(outfile))
{
zf.AddFile(toCompress,"");
zf.Save();
}
return outfile;
}
最终,我要做的是进行检查,以查看在创建zip文件之后是否未将任何xml文件添加到zip文件中,但是总体上最好将问题停在它的轨道上。 谢谢您的帮助。
确保在try ... catch语句中包含该代码。 此外,请确保已完成该操作,但例外。 具有这种类型的异常处理的情况并不是第一种情况:
try
{
//...
}
catch { }
鉴于上面的代码,如果您的过程中有任何异常,您将永远不会注意到。
仅凭此函数很难判断,这里列出了可能出错的地方:-在调用zf.AddFile时,toCompress文件可能已消失(但在Exists测试之后)。 测试返回值或添加异常处理以检测到这一点。 -zip outFile可能刚好小于大小限制,添加新文件可以使其超过限制。 -AddToZip()可能被同时调用,这可能导致添加失败。
如何删除toCompress文件? 我认为在函数作用域上向AddoZip()添加锁定可能也是个好主意。
这可能是时间问题。 您在尝试添加toCompress文件之前正在检查outfile是否太大。 您应该做的是:
我怀疑您有时会有一个超出限制的输出文件,但是添加到Compress会把它移走。 然后,接收程序不会处理输出文件,因为文件太大。
我可能完全不在基地,但这是需要检查的东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.