2017-02-08 51 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樂於幫忙。 –