我使用了以下代码,它运行正常。但有时会抛出异常:
Message :- The process cannot access the file 'DownloadDB\client\user\filename.db' because it is being used by another process.
Stack Trace :-
at System.IO.File.InternalDelete(String path, Boolean checkHost)
at System.IO.File.Delete(String path)
at Ionic.Zip.ZipEntry.InternalExtract(String baseDir, Stream outstream, String password)
at Ionic.Zip.ZipEntry.Extract(String baseDirectory)
at Ionic.Zip.ZipFile._InternalExtractAll(String path, Boolean overrideExtractExistingProperty)
at Ionic.Zip.ZipFile.ExtractAll(String path, ExtractExistingFileAction extractExistingFile)
at ecpMobileToWebSync.UnZipFiles(String strZipFilePath, String CombinePath, String fk_EmpGLCode)
我的代码如下:
private string SyncDB(string FileArray)
{
string pathNew = "";
byte[] encodedDataAsBytesSyncDB = System.Convert.FromBase64String(FileArray);
pathNew = Server.MapPath("~/SyncDB/" + strClientName_File + "/" + UserName + "/" + Filename);
File.WriteAllBytes(pathNew, encodedDataAsBytesSyncDB);
byte[] encodedDataAsBytesDownloadDB = System.Convert.FromBase64String(FileArray);
pathNew = Server.MapPath("~/DownloadDB/" + strClientName_File + "/" + UserName + "/" + Filename);
File.WriteAllBytes(pathNew, encodedDataAsBytesDownloadDB);
encodedDataAsBytesSyncDB = null;
encodedDataAsBytesDownloadDB = null;
FileArray = null;
string strFileName = string.Empty;
if (File.Exists(pathNew))
{
strFileName = UnZipFiles(pathNew, Server.MapPath("~/DownloadDB/" + strClientName_File + "/" + UserName + "/"));
}
}
private string UnZipFiles(string strZipFilePath, string CombinePath)
{
string strZilEntryFileName = string.Empty;
ReturnData objReturnData = new ReturnData();
objReturnData.isValid = true;
objReturnData.Message = "";
try
{
using (ZipFile zip1 = ZipFile.Read(strZipFilePath))
{
strZilEntryFileName = zip1.Entries.Single().FileName;
var flattenFoldersOnExtract = zip1.FlattenFoldersOnExtract;
zip1.FlattenFoldersOnExtract = true;
zip1.ExtractAll(CombinePath, ExtractExistingFileAction.OverwriteSilently);
zip1.FlattenFoldersOnExtract = flattenFoldersOnExtract;
}
}
catch (Exception ex)
{
ErrorLogDetails.LogException(this.GetType().Name.ToString(), System.Reflection.MethodBase.GetCurrentMethod().Name.ToString());
objReturnData.isValid = false;
objReturnData.Message = MsgException_Failure;
}
return strZilEntryFileName;
}
在这段代码中,我从客户端获取一个zip文件的字节数组,然后我执行逆向工程,将字节数组转换为zip文件,然后将其解压缩。
首先,我将使用字节数组调用SyncDB函数,然后调用UnZipFiles来解压缩文件。
这一切都很好,但在某些情况下会有例外。