2017-02-08 2 views
2

손상된 .docx 파일을 고전 ASP로 복구하려고합니다.손상된 docx 파일을 프로그래밍 방식으로 수정하는 방법 (누락 바이트 추가)

(파일 끝에는 바이트가 누락되었습니다 (자세한 내용은 this question 참조).

파일을 Sublime (16 진수보기로 표시)에서 보면 0000을 파일 끝에 추가하여 손상을 수정할 수 있습니다.

enter image description here

하지만 프로그래밍, 마지막에 그 4 0을 추가하기 위해 고군분투하고있다.

나는 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) 

나는 .AddBytes() 방법에 0000 값을 얻는 방법 작동하지 않을 수 있습니다.

어떻게하면됩니까? 나는 내 깊이에서 조금 벗어났다. 그리고 내가 올바른 방향으로 접근하고 있는지 확신 할 수 없다. 나의 무지에서


, 여기에 내가 시도 내용은 다음과 같습니다 (나는 0000이 null 값을 나타냅니다 생각하기 때문에) 빈 추가 바이트를 떠나 ByteArray을 Redimming


.

이 파일을 전혀 변경하지 않는 것 같습니다. 새 저장된 파일은 이전 파일과 동일합니다.

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

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

Call SaveBytesToBinaryFile(ByteArray, sNewFilePath) 

바이트 진수에서 0000 변환하고 손상된 파일 바이트에 추가.

다시 말하지만 파일을 전혀 변경하지 않는 것 같습니다.


손상된 파일의 최종 바이트의 도착하고,이 ByteArray를 redimming 후이 새로운 값에 추가

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) 
.

이 파일을 전혀 변경하지 않은 것 같습니다!

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()'를 사용하여 추가 할 수 있어야합니다.'Call .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

@ MartinsHansenLennox 기꺼이 도와 드리겠습니다. –

관련 문제