尝试从字节流打开 docx 文件 - 文件损坏错误

发布于 2024-08-21 16:58:56 字数 1918 浏览 5 评论 0原文

从保存的字节流打开 docx 文件时,我们始终收到文件损坏的错误消息,所有其他文件类型都可以正常工作,

下面是来自示例表单的代码,可重现该问题

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    'Objective is to be able to copy a file to a bytestream then create a new document from that stream  and then opne it.
    'This replicates the behaviour of our primary application where it stores and retrieves the stream from a database
    'With docx files we consistently experience some sort of corruption in the write of the original file
    'When selecting .doc or other format files we do not encounter the same problem

    'use selected file
    Dim _o1 As String = TextBox1.Text
    'get its bytestream
    Dim fs As New FileStream(_o1, FileMode.Open, FileAccess.Read)
    Dim byteStream(Convert.ToInt32(fs.Length)) As Byte
    fs.Read(byteStream, 0, Convert.ToInt32(fs.Length))

    'create a new file and use the bytestream to create it and save to disk
    Dim _o As String = "C:\" & Now.Ticks & getNewFileName()

    Dim fs1 As New FileStream(_o, FileMode.OpenOrCreate, FileAccess.Write)
    Using bw As New BinaryWriter(fs1)
        bw.Write(byteStream)
        bw.Flush()
    End Using

    'open the new document
    System.Diagnostics.Process.Start(_o)
    Application.DoEvents()
End Sub
Private Function getNewFileName() As String
    Dim fi As New FileInfo(TextBox1.Text)

    Return Now.Ticks.ToString & fi.Name
End Function
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    OpenFileDialog1.InitialDirectory = "c:\"
    OpenFileDialog1.FilterIndex = 2
    OpenFileDialog1.RestoreDirectory = True
    OpenFileDialog1.Filter = "docx files |*.docx"

    If OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
        TextBox1.Text = OpenFileDialog1.FileName

    End If

End Sub

we consistently get a file corrupted error message when opening a docx file from a saved bytestream, evry other file type works just ok

Below is code from a sample form that replciate the issue

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    'Objective is to be able to copy a file to a bytestream then create a new document from that stream  and then opne it.
    'This replicates the behaviour of our primary application where it stores and retrieves the stream from a database
    'With docx files we consistently experience some sort of corruption in the write of the original file
    'When selecting .doc or other format files we do not encounter the same problem

    'use selected file
    Dim _o1 As String = TextBox1.Text
    'get its bytestream
    Dim fs As New FileStream(_o1, FileMode.Open, FileAccess.Read)
    Dim byteStream(Convert.ToInt32(fs.Length)) As Byte
    fs.Read(byteStream, 0, Convert.ToInt32(fs.Length))

    'create a new file and use the bytestream to create it and save to disk
    Dim _o As String = "C:\" & Now.Ticks & getNewFileName()

    Dim fs1 As New FileStream(_o, FileMode.OpenOrCreate, FileAccess.Write)
    Using bw As New BinaryWriter(fs1)
        bw.Write(byteStream)
        bw.Flush()
    End Using

    'open the new document
    System.Diagnostics.Process.Start(_o)
    Application.DoEvents()
End Sub
Private Function getNewFileName() As String
    Dim fi As New FileInfo(TextBox1.Text)

    Return Now.Ticks.ToString & fi.Name
End Function
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    OpenFileDialog1.InitialDirectory = "c:\"
    OpenFileDialog1.FilterIndex = 2
    OpenFileDialog1.RestoreDirectory = True
    OpenFileDialog1.Filter = "docx files |*.docx"

    If OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
        TextBox1.Text = OpenFileDialog1.FileName

    End If

End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

Bonjour°[大白 2024-08-28 16:58:56

请原谅我,但这是一些混乱的代码。

Dim _o As String = "C:\" & Now.Ticks & getNewFileName()

将变成...

Dim _o As String = "C:\" & Now.Ticks & Now.Ticks.ToString & fi.Name

示例结果 "C:\" "634015010433498951" "634015010433498951" "FileName.txt" 可能不是您所期望的,除非您打算减去两个刻度计数来确定填充 FileInfo 所需的时间。

您的 FileStream 损坏可能是编码问题、一个文件长度问题,甚至深层路径中的长文件名也可能是问题。这段代码应该可以正常工作,而不是使用 FileStream:

Dim sourceFile As String = TextBox1.text
Dim fi As New System.IO.FileInfo(sourceFile)
Dim destFile = "C:\" & Now.Ticks & fi.Name
fi.CopyTo(destFile)
'open the new document   
System.Diagnostics.Process.Start(destFile)

Forgive me, but that is some messed up code.

Dim _o As String = "C:\" & Now.Ticks & getNewFileName()

will become...

Dim _o As String = "C:\" & Now.Ticks & Now.Ticks.ToString & fi.Name

Example result "C:\" "634015010433498951" "634015010433498951" "FileName.txt" is probably not what you are expecting unless you intend to subtract the two tick counts to determine how long it took to populate FileInfo.

Your FileStream corruption could be a encoding issue, off by one file length issue, or even a long filename in a deep path could a problem. Instead of using FileStream, this code should work fine:

Dim sourceFile As String = TextBox1.text
Dim fi As New System.IO.FileInfo(sourceFile)
Dim destFile = "C:\" & Now.Ticks & fi.Name
fi.CopyTo(destFile)
'open the new document   
System.Diagnostics.Process.Start(destFile)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文