2017-02-08 53 views
2

我试图修复经典asp中大量损坏的.docx文件。如何以编程方式修复损坏的docx文件(添加丢失的字节)

(这些文件在最后丢失字节 - 详见this question)。

当我在Sublime中查看文件(以十六进制视图显示它)时,可以通过将0000添加到文件的末尾来修复损坏。

enter image description here

但我很努力的4个零添加到年底,编程。

我试图使用cByteArray类,它使用的是这样的:

With oByte 
    Call .AddBytes(LoadBytes(sFilePath)) 
    Call .AddBytes(HOW DO I GET THE BYTE VALUE OF 0000 HERE?) 
    lngBytes = .BytesTotal 
    ByteArray = .ReturnBytes 
End With 

Call SaveBytesToBinaryFile(ByteArray, sNewFilePath) 

我无法工作,如何让0000值到.AddBytes()方法。

我该怎么做?我有点出于自己的深度,并不确定我是否以正确的方式接近这一点。


在我的无知,这是我曾尝试:


Redimming ByteArray留下额外的字节空的(因为我觉得0000表示空值)。

这似乎并没有改变文件。新保存的文件与旧文件相同。

With oByte 
    Call .AddBytes(LoadBytes(sFilePath)) 
    ByteArray = .ReturnBytes 
End With 

arrayLength = ubound(ByteArray) 
redim ByteArray(arrayLength + 2) 

Call SaveBytesToBinaryFile(ByteArray, sNewFilePath) 

从十六进制转换0000到字节并将其添加到已损坏的文件字节。

此外,这似乎并没有改变文件。

dim k, hexString, str, stream, byteArrToAdd 
hexString = "000000" 
For k = 1 To Len(hexString) Step 2 
str = str & Chr("&h" & Mid(hexString, k, 2)) 
response.write "<hr />" & str & "<hr />" 
Next 

Set stream = CreateObject("ADODB.Stream") 
With stream 
.Open 
.Type = 2  ' set type "text" 
.WriteText str 
.Position = 0 
.Type = 1  ' change type to "binary" 
byteArrToAdd = .Read 
.Close 
End With 
set stream = nothing 

With oByte 
    Call .AddBytes(LoadBytes(sFilePath)) 
    Call .AddBytes(byteArrToAdd) 
    ByteArray = .ReturnBytes 
End With 

Call SaveBytesToBinaryFile(ByteArray, sNewFilePath) 

获取损坏的文件的最后一个字节,并redimming的ByteArray之后将其添加到2个新值。

这似乎并没有改变文件在所有!

With oByte 
    Call .AddBytes(LoadBytes(sFilePath)) 
    ByteArray = .ReturnBytes 
End With 


arrayLength = ubound(ByteArray) 
finalByte = ByteArray(arrayLength) 
redim ByteArray(arrayLength + 2) 
ByteArray(arrayLength + 1) = finalByte 
ByteArray(arrayLength + 2) = finalByte 

Call SaveBytesToBinaryFile(ByteArray, sNewFilePath) 
+0

您应该能够使用附加这些'CHRB()',像'呼叫.AddBytes(CHRB(H00)CHRB(H00))'应该工作。 – Lankymart

+0

我无法得到这个工作。 “ControlChars.NullChar”的同上 - 既不添加任何字节到文件。你认为它与此有关吗? http://www.vbforums.com/showthread.php?628175-CHR(-amp-H00)-vs-CHR-(-amp-H00)(遗憾的是,没有提供解决方案) –

+0

我很抱歉,你的男人。 ..2017年11月仍然使用经典的asp:/ – 2Noob2Good

回答

2

您可以使用一个二进制文件流与用户定义的转换(字符串字节()),如下面的函数的帮助。

Function GetBytesOf(str) 'returns bytes of given string 
    With CreateObject("Adodb.Stream") 
     .Type = 2 'text 
     .Charset = "x-ansi" 
     .Open 
     .WriteText str 
     .Position = 0 
     .Type = 1 'binary 
     GetBytesOf = .Read 'returns Byte() 
     .Close 
    End With 
End Function 

Dim patch 
patch = GetBytesOf(Chr(0) & Chr(0)) 'equals to WORD 0000 

With CreateObject("Adodb.Stream") 
    .Type = 1 'binary 
    .Open 
    .LoadFromFile sFilePath 
    'move cursor to the end of file 
    .Position = .Size 
    .Write patch 
    .SaveToFile sNewFilePath, 2 '2 for overwrite if exists 
    .Close 
End With 
+1

非常感谢。这比我尝试过的任何东西都简单得多,而且完美地工作。很高兴:) –

+0

@MartinHansenLennox乐于帮忙。 –