C# - 复制文件并恢复支持
你好 有没有人有可行的示例代码来复制文件并支持在源已断开连接时恢复文件?
在此示例中,我正在复制视频文件。如果源已断开(即 USB 已拔出),我如何支持再次恢复它们?我在 stackoverflow 上尝试了一些代码,但恢复后,视频文件似乎已损坏。 FileStream 是视频传输/恢复的最佳解决方案吗?
欢迎任何其他指示或提示。
private void CreateNewCopyTo(FileStream source, FileStream dest) {
int size = (source.CanSeek) ? Math.Min((int)(source.Length - source.Position), 0x2000) : 0x2000;
byte[] buffer = new byte[size];
int read;
long fileLength = source.Length;
while ((read = source.Read(buffer, 0, buffer.Length)) > 0) {
dest.Write(buffer, 0, read);
dest.Close();
dest = new FileStream(dest.Name, FileMode.Append, FileAccess.Write);
}
dest.Close();
}
private void ResumeCopyTo(FileStream source, FileStream dest) {
int size = (source.CanSeek) ? Math.Min((int)(source.Length - source.Position), 0x2000) : 0x2000;
byte[] buffer = new byte[size];
long TempPos = source.Position;
while (true) {
int read = source.Read(buffer, 0, buffer.Length);
if (read <= 0)
return;
dest.Write(buffer, 0, read);
dest.Close();
dest = new FileStream(dest.Name, FileMode.Append, FileAccess.Write);
}
}
Hi
Does anyone have workable sample code that copy files and support resuming them if there are the source has been disconnected?
In this example I am copying videos files. If the source is disconnected ie usb was unplugged, how can I support resuming them again? I have tried some code on stackoverflow, but after resuming, the video files seem to be corrupted. Is FileStream the best solution for video transfers/resume?
Any other pointers or tips are welcome.
private void CreateNewCopyTo(FileStream source, FileStream dest) {
int size = (source.CanSeek) ? Math.Min((int)(source.Length - source.Position), 0x2000) : 0x2000;
byte[] buffer = new byte[size];
int read;
long fileLength = source.Length;
while ((read = source.Read(buffer, 0, buffer.Length)) > 0) {
dest.Write(buffer, 0, read);
dest.Close();
dest = new FileStream(dest.Name, FileMode.Append, FileAccess.Write);
}
dest.Close();
}
private void ResumeCopyTo(FileStream source, FileStream dest) {
int size = (source.CanSeek) ? Math.Min((int)(source.Length - source.Position), 0x2000) : 0x2000;
byte[] buffer = new byte[size];
long TempPos = source.Position;
while (true) {
int read = source.Read(buffer, 0, buffer.Length);
if (read <= 0)
return;
dest.Write(buffer, 0, read);
dest.Close();
dest = new FileStream(dest.Name, FileMode.Append, FileAccess.Write);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试插入
source.Seek(dest.Length, SeekOrigin.Begin);
作为 ResumeCopyTo 方法中的第一行。Try to insert
source.Seek(dest.Length, SeekOrigin.Begin);
as first line in ResumeCopyTo method.创建一个与您复制的文件大小相同的文件。如果连接断开,请在文件中写入“书签”。文件末尾的一些任意字符串可以告诉您停止的位置。
最好获取原始文件的哈希值,以确定自上次复制尝试以来该文件是否已更改。 (如果文件不同,那么您应该检查目标中的数据与源中的数据,也许还有数据的另一个哈希值。)
最重要的部分是在源和目标中寻找完全相同的点在两个文件中。在恢复尝试之前。
您需要确保您的查找将为您提供源文件中的下一个字节。与您上次留下的字节不同。否则,您的数据将只有一个重复字符。这会损坏数据。
(例如,如果源文件是 12345678901234567890 并且您的目的地已到达 123456 ,您不想在 6 点恢复,您想在 7 点恢复。否则您的目的地将为 123456678901234567890 )
create a file that is the same size as the file your copying. And if the connection is broken write a "BookMark" into the file. Some arbitrary string at the end of the file that tells you where you left off.
Best to get a hash of the original file to determine if the file has changed since the last copy attempt. (if the file is different then you should check the data in the destination with the data in the source, perhaps with another hash of the data.)
The most important part tho is to seek in the source and the destination to the exact same point in both files. before the resume attempt.
You will want to make sure that your seek will give you the next byte in the source file. Not the same byte you left off at. Otherwise you will be left with your data having a single duplicate character. Which will corrupt the data.
(e.g. if the source file is 12345678901234567890 and your destination made it to 123456 , you dont want to resume at 6, you want to resume at 7. otherwise your destination would be 123456678901234567890)
假设文件没有及时更改,我们可以使用您编写的函数。然而,它们应该稍微修改并简化为一个函数。
我们可以创建一个附加功能:
生产环境的想法:
顺便说一句:
就这样,我通过质量很差的网络连接,在9个小时内复制了一个15GB的文件。
Assuming that the file does not change in time, we can use the functions that you have written. However, they should be slightly modified and simplified to one function.
We can create an additional function:
Ideas for a production environment:
BTW:
In this way, I copied an 15 GB file in 9 hours via a network connection of really poor quality.