上传压缩文件时偶尔出现ftp 450错误

时间:2022-11-25 03:44:50

I have an 800Mb file that's getting compressed using the System.IO.Compression ZipArchive, thus:

我有一个800Mb文件,使用System.IO.Compression ZipArchive进行压缩,因此:

using (ZipArchive zip = ZipFile.Open(result, ZipArchiveMode.Create))
{
    zip.CreateEntryFromFile(sourceFile, entryName);
}

The file compression is always successful. Once the file has been compressed, it's being uploaded to an ftp server with variable success. When the upload fails, it does so with the following error:

文件压缩始终成功。文件被压缩后,它将被上传到ftp服务器并获得成功。上传失败时,会出现以下错误:

The remote server returned an error: (450) File unavailable (e.g., file busy).

远程服务器返回错误:(450)文件不可用(例如,文件繁忙)。

Stack Trace: at System.Net.FtpWebRequest.SyncRequestCallback(Object obj) at System.Net.CommandStream.Dispose(Boolean disposing) at System.IO.Stream.Close() at System.Net.ConnectionPool.Destroy(PooledStream pooledStream) at System.Net.ConnectionPool.PutConnection(PooledStream pooledStream, Object owningObject, Int32 creationTimeout, Boolean canReuse) at System.Net.FtpWebRequest.FinishRequestStage(RequestStage stage) at System.Net.FtpWebRequest.SyncRequestCallback(Object obj) at System.Net.CommandStream.Abort(Exception e) at System.Net.CommandStream.CheckContinuePipeline() at System.Net.FtpDataStream.System.Net.ICloseEx.CloseEx(CloseExState closeState) at System.Net.FtpDataStream.Dispose(Boolean disposing) at System.IO.Stream.Close()

堆栈跟踪:位于System.Net.ConnectionPool.Destroy(PooledStream pooledStream)的System.IO.Stream.Close()处System.Net.CommandStream.Dispose(布局处理)的System.Net.FtpWebRequest.SyncRequestCallback(Object obj)处在System.Net.CommandStream的System.Net.FtpWebRequest.SyncRequestCallback(Object obj)的System.Net.FtpWebRequest.FinishRequestStage(RequestStage阶段)的System.Net.ConnectionPool.PutConnection(PooledStream pooledStream,Object owningObject,Int32 creationTimeout,Boolean canReuse) .Abort(例外e)System.Net上的System.Net.FtpDataStream.System.Net.ICloseEx.CloseEx(CloseExState closeState)上的System.Net.CommandStream.CheckContinuePipeline()处于System.Net的System.Net.FtpDataStream.Dispose(布尔处理) .Stream.Close()

If I upload the uncompressed original file instead, that upload is always successful.

如果我上传未压缩的原始文件,那么上传总是成功的。

My suspicion is that the compressed file is somehow locked, or the ZipArchive instance may not have been disposed. I have tried alternative compression libraries with similarly inconsistent results. Any ideas?

我怀疑压缩文件是以某种方式锁定的,或者ZipArchive实例可能没有被丢弃。我尝试过具有类似不一致结果的替代压缩库。有任何想法吗?

1 个解决方案

#1


0  

The reason for the error turned out to be nothing to do with my initial suspicions, but was related to the ftp upload request. As a zip file is a binary format, it's necessary to set the request to use binary, and to avoid using a StreamReader to get the bytes from the file. My ftp code ended up like this:

错误的原因与我最初的怀疑无关,但与ftp上传请求有关。由于zip文件是二进制格式,因此必须设置使用二进制的请求,并避免使用StreamReader从文件中获取字节。我的ftp代码最终如下:

FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri('ftpurl', filePath)));
request.Timeout = timeoutvalue;
request.Method = WebRequestMethods.Ftp.UploadFile;
request.UseBinary = true;
request.Proxy = new WebProxy();
request.Credentials = new NetworkCredential('x','y');

byte[] encodedFileContents = File.ReadAllBytes(filePath);
request.ContentLength = encodedFileContents.Length;

using (Stream requestStream = request.GetRequestStream())
{
    requestStream.WriteTimeout = timeoutvalue;
    requestStream.Write(encodedFileContents, 0, encodedFileContents.Length);
}

using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
{
}

#1


0  

The reason for the error turned out to be nothing to do with my initial suspicions, but was related to the ftp upload request. As a zip file is a binary format, it's necessary to set the request to use binary, and to avoid using a StreamReader to get the bytes from the file. My ftp code ended up like this:

错误的原因与我最初的怀疑无关,但与ftp上传请求有关。由于zip文件是二进制格式,因此必须设置使用二进制的请求,并避免使用StreamReader从文件中获取字节。我的ftp代码最终如下:

FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri('ftpurl', filePath)));
request.Timeout = timeoutvalue;
request.Method = WebRequestMethods.Ftp.UploadFile;
request.UseBinary = true;
request.Proxy = new WebProxy();
request.Credentials = new NetworkCredential('x','y');

byte[] encodedFileContents = File.ReadAllBytes(filePath);
request.ContentLength = encodedFileContents.Length;

using (Stream requestStream = request.GetRequestStream())
{
    requestStream.WriteTimeout = timeoutvalue;
    requestStream.Write(encodedFileContents, 0, encodedFileContents.Length);
}

using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
{
}