Copy a file in little chunks

前端 未结 1 1983
囚心锁ツ
囚心锁ツ 2021-01-16 12:39

I want to copy a file in little chunks (to cancel the copy operation if needed).

I\'m trying to follow the unmarked solution here: How to copy a file with the abilit

相关标签:
1条回答
  • 2021-01-16 13:35

    You have to understand what you're doing and how it works.

    First, you allocate a buffer.

    Dim buffer = New Byte(1024) {}
    

    Then you go and read some data from the input stream. The data is put in buffer, starting at 0, at most buffer.Length bytes. The method returns how many bytes it has actually read and put in the buffer.

    bytesRead = inputStream.Read(buffer, 0, buffer.Length)
    

    If the number of bytes you've read is bigger than 0, then you haven't reached the end of the file yet.

    While (bytesRead > 0)
    

    Then you write exactly those bytes that were read (bytesRead bytes) to the output stream. Write the bytes from buffer, start at index 0 and write bytesRead number of bytes.

    outputStream.Write(buffer, 0, bytesRead)
    

    Since the stream will buffer what you wrote for efficiency reasons, you'll have to flush and close the output stream.

    outputStream.Flush()
    outputStream.Close()
    

    Putting it together:

    Dim buffer = New Byte(1024) {}
    Dim bytesRead As Integer
    
    ' Read some bytes
    While ((bytesRead = inputStream.Read(buffer, 0, buffer.Length)) > 0)
        ' Write them to the output
        outputStream.Write(buffer, 0, bytesRead)
    
        If cancelled Then
            MsgBox("operacion cancelada")
            Return
        End If
    
        ' Repeat
    End
    
    outputStream.Flush()
    outputStream.Close()
    

    The last time I wrote VB is more than a decade ago. You'll have to ensure the syntax is correct.


    Notice how the original code contains this line:

    While ((bytesRead = inputStream.Read(buffer, 0, buffer.Length)) > 0)
    

    You see the bytesRead = part? You haven't copied that into your code. It is essential to store the number of bytes read.


    Final code: this works on my computer:

    Imports System
    Imports System.IO
    
    Namespace ConsoleApplication1
        Friend Class Program
            Private Shared Sub Main(args As String())
                Program.CopyMyFiles()
                Console.WriteLine("DONE!")
                Console.ReadLine()
            End Sub
    
            Private Shared Sub CopyMyFiles()
                Dim input_filepath As String = "Test.txt"
                Dim output_filepath As String = "Test_New.txt"
                Dim input As FileStream = New FileStream(input_filepath, FileMode.Open, FileAccess.ReadWrite)
                Dim output As FileStream = New FileStream(output_filepath, FileMode.Create, FileAccess.ReadWrite)
                Program.CopyStream(input, output)
            End Sub
    
            Public Shared Sub CopyStream(inputStream As Stream, outputStream As Stream)
                Dim buffer As Byte() = New Byte(1025)
                Dim bytesRead As Integer
                bytesRead = inputStream.Read(buffer, 0, buffer.Length)
                While bytesRead > 0
                    outputStream.Write(buffer, 0, bytesRead)
                    bytesRead = inputStream.Read(buffer, 0, buffer.Length)
                End While
                outputStream.Flush()
                inputStream.Close()
                outputStream.Close()
            End Sub
        End Class
    End Namespace
    
    0 讨论(0)
提交回复
热议问题