Microsoft MVP성태의 닷넷 이야기
c# 압축파일 읽어 올 때 BinaryRead 한글 처리 문제 [링크 복사], [링크+제목 복사],
조회: 10424
글쓴 사람
김영식 (nukkims85 at gmail.com)
홈페이지
첨부 파일
 

using (var fileStream = File.OpenRead( filePath ) )
using (var bodyStream = new ZlibStream( fileStream, CompressionMode.Decompress))
using (var bodyReader = new BinaryReader(bodyStream))
{
   fileStream.Seek(header.FileTablePosition + 8, SeekOrigin.Begin);
   for (int i = 0; i < header.FileCount; i++)
   {
      var fileName = string.Empty;
      char currentChar;
      while ((currentChar = (char)bodyReader.ReadByte()) != 0)
      {
         fileName += currentChar;
      }
   }
   .
   .
   .
}

위의 코드는 윈도우에서 압축파일을 BinaryReader의 ReadByte()를 이용해 byte단위로 읽어 와서 char형으로 변환하고
그걸 합해서 file이름으로 가져오는 부분인데요. 이걸 실행하면 파일이름이 한글로 되어 있으면 깨져 나옵니다. currentChar를 누적하니까
당연한거 같기도 합니다.

일단 BinaryReader(bodyStream)의 인코딩 옵션을 줘봤는데 안되더군요. 뭐 당연한거 같습니다만.

그리고 최종 fileName값의 인코딩 변환

Encoding utf8 = Encoding.UTF8;
byte[] utf8Bytes = utf8.GetBytes(fileName);
Han_FileName = utf8.GetString(utf8Bytes);

이렇게 수정해 디버그 상태에서 값을 봤는데 안되내요.

저 부분을 어떻게 수정하면 한글 파일이름을 인식하게 할 수 있을까요?













[최초 등록일: ]
[최종 수정일: 12/26/2022]


비밀번호

댓글 작성자
 



2022-12-27 01시27분
그런 경우 Decoder를 쓰는 것이 좋지 않을까요?

한글이 포함된 바이트 배열을 나눈 경우 한글이 깨지지 않도록 다시 조합하는 방법
; https://www.sysnet.pe.kr/2/0/11378

아니면, byte로 읽어 char로 즉시 변환하지 말고 모두 읽은 다음에 Decoding을 해야 합니다.
정성태

NoWriterDateCnt.TitleFile(s)