Microsoft MVP성태의 닷넷 이야기
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일

윈도우 탐색기에서 열리지 않는 zip 파일 - The Compressed (zipped) Folder '[...].zip' is invalid.

이상하군요, 웹 서버 측에서 코드로 압축해 내려준 ZIP 파일이 Windows Explorer에서 다음과 같은 오류를 내며 풀리지 않습니다.

Compressed (zipped) Folders Error

Windows cannot complete the extraction.

The Compressed (zipped) Folder 'C:\temp\test.zip' is invalid.

전에 한 번 유사한 경험을 해봐서,

자바와 닷넷의 압축 호환
; https://www.sysnet.pe.kr/2/0/724

C# - Deflate, GZip, Zip
; https://www.sysnet.pe.kr/2/0/723

이번에도 파고 들어보면 해결책이 나오지 않을까 싶어 좀 살펴봤습니다. ^^




우선, Alzip이나 PowerShell의 Expand-Archive 명령어로는 잘 풀립니다.

PS> Expand-Archive test.zip -DestinationPath c:\temp\test

도대체 무슨 차이일까요? 혹시 세세한 ZIP 포맷에 어떤 문제가 있을까 싶어 WSL을 경유해 zipdetails를 이용해 살펴보면,

// 탐색기에서 오류가 발생하는 zip 파일

$ zipdetails  /mnt/c/temp/test.zip

000000 LOCAL HEADER #1       04034B50
000004 Extract Zip Spec      14 '2.0'
000005 Extract OS            00 'MS-DOS'
000006 General Purpose Flag  0808
       [Bits 1-2]            0 'Normal Compression'
       [Bit  3]              1 'Streamed'
       [Bit 11]              1 'Language Encoding'
000008 Compression Method    0008 'Deflated'
00000A Last Mod Time         5632700B 'Wed Jan 18 14:00:22 2023'
00000E CRC                   00000000
000012 Compressed Length     00000000
000016 Uncompressed Length   00000000
00001A Filename Length       001D
00001C Extra Length          0000
00001E Filename              '/invalid_file/test/unk00.json'                              
00003B PAYLOAD

001A78 STREAMING DATA HEADER 08074B50
001A7C CRC                   F95C9612
001A80 Compressed Length     00001A3D
001A84 Uncompressed Length   0000776B

001A88 LOCAL HEADER #2       04034B50
001A8C Extract Zip Spec      14 '2.0'
001A8D Extract OS            00 'MS-DOS'
001A8E General Purpose Flag  0808
       [Bits 1-2]            0 'Normal Compression'
       [Bit  3]              1 'Streamed'
       [Bit 11]              1 'Language Encoding'
001A90 Compression Method    0008 'Deflated'
001A92 Last Mod Time         5632700C 'Wed Jan 18 14:00:24 2023'
001A96 CRC                   00000000
001A9A Compressed Length     00000000
001A9E Uncompressed Length   00000000
001AA2 Filename Length       001D
001AA4 Extra Length          0000
001AA6 Filename              '/invalid_file/test/unk01.json'
001AC3 PAYLOAD

...[생략]...

// 탐색기에서 오류가 없는 zip 파일

$ zipdetails /mnt/c/temp/test2.zip

0000 LOCAL HEADER #1       04034B50
0004 Extract Zip Spec      14 '2.0'
0005 Extract OS            00 'MS-DOS'
0006 General Purpose Flag  0000
     [Bits 1-2]            0 'Normal Compression'
0008 Compression Method    0008 'Deflated'
000A Last Mod Time         56CD5886 'Tue Jun 13 11:04:12 2023'
000E CRC                   FF16EA5E
0012 Compressed Length     00000109
0016 Uncompressed Length   000001EA
001A Filename Length       000D
001C Extra Length          0000
001E Filename              'testfile1.txt'
002B PAYLOAD

0134 LOCAL HEADER #2       04034B50
0138 Extract Zip Spec      14 '2.0'
0139 Extract OS            00 'MS-DOS'
013A General Purpose Flag  0000
     [Bits 1-2]            0 'Normal Compression'
013C Compression Method    0008 'Deflated'
013E Last Mod Time         56CD58D5 'Tue Jun 13 11:06:42 2023'
0142 CRC                   1D1EC9E8
0146 Compressed Length     00000381
014A Uncompressed Length   000004B8
014E Filename Length       000D
0150 Extra Length          0000
0152 Filename              'testfile2.txt'
015F PAYLOAD

...[생략]...

열리지 않는 파일이 "STREAMING DATA HEADER"를 가지고 있습니다. 과연 이게 문제일까요?




자, 그래서 테스트를 해봤습니다. 이를 위해 다음의 Q&A에 있는 C# 코드를 이용해,

.net core unzip a file and zip subfolder
; https://stackoverflow.com/questions/53645337/net-core-unzip-a-file-and-zip-subfolder

문제가 되었던 test.zip 파일을 ZipArchive로 열어, 그대로 다시 ZipArchive로 복사해 저장하는 역할만 하도록 바꿀 수 있습니다.

using System.IO.Compression;

namespace ConsoleApp1;

// https://stackoverflow.com/questions/53645337/net-core-unzip-a-file-and-zip-subfolder

internal class Program
{
    static void Main(string[] args)
    {
        using (MemoryStream srcMemoryStream = new MemoryStream())
        using (MemoryStream targetMemoryStream = new MemoryStream())
        {
            using (FileStream sourceZipFile = new FileStream(@"C:\temp\test.zip", FileMode.Open))
            {
                sourceZipFile.CopyTo(srcMemoryStream);
            }

            using (ZipArchive srcArchive = new ZipArchive(srcMemoryStream, ZipArchiveMode.Read))
            using (ZipArchive destArchive = new ZipArchive(targetMemoryStream, ZipArchiveMode.Create, true))
            {

                srcArchive.Entries
                    .ToList()
                    .ForEach((entry) =>
                    {
                        ZipArchiveEntry newEntry = destArchive.CreateEntry(entry.FullName);
                        using (Stream srcEntry = entry.Open())
                        {
                            using (Stream destEntry = newEntry.Open())
                            {
                                srcEntry.CopyTo(destEntry);
                            }
                        }
                    });
            }

            using (FileStream fs = new FileStream(@"c:\temp\test3.zip", FileMode.Create))
            {
                targetMemoryStream.WriteTo(fs);
                targetMemoryStream.Flush();
                fs.Flush(true);
            }
        }
    }
}

위의 결과로 저장된 test3.zip 파일은 윈도우 탐색기에서도 잘 열렸던 포맷 유형으로 바뀝니다.


$ zipdetails /mnt/c/temp/test3.zip

000000 LOCAL HEADER #1       04034B50
000004 Extract Zip Spec      14 '2.0'
000005 Extract OS            00 'MS-DOS'
000006 General Purpose Flag  0000
       [Bits 1-2]            0 'Normal Compression'
000008 Compression Method    0008 'Deflated'
00000A Last Mod Time         56E37165 'Mon Jul  3 14:11:10 2023'
00000E CRC                   F95C9612
000012 Compressed Length     00001A95
000016 Uncompressed Length   0000776B
00001A Filename Length       001D
00001C Extra Length          0000
00001E Filename              '/invalid_file/test/unk00.json'
00003B PAYLOAD

001AD0 LOCAL HEADER #2       04034B50
001AD4 Extract Zip Spec      14 '2.0'
001AD5 Extract OS            00 'MS-DOS'
001AD6 General Purpose Flag  0000
       [Bits 1-2]            0 'Normal Compression'
001AD8 Compression Method    0008 'Deflated'
001ADA Last Mod Time         56E37165 'Mon Jul  3 14:11:10 2023'
001ADE CRC                   3A6DE765
001AE2 Compressed Length     00001487
001AE6 Uncompressed Length   000061E2
001AEA Filename Length       001D
001AEC Extra Length          0000
001AEE Filename              '/invalid_file/test/unk01.json'

그리고, 이렇게 변형된 파일 역시 윈도우 탐색기에서는 열리지 않았습니다. 다른 문제가 있다는 것이죠? ^^




본문의 예제 파일을 눈여겨보신 분이라면, "Filename"의 시작이 "/"로 되는 특징을 하나 발견할 수 있습니다. 실제로 이것 때문인지 (Windows 10부터 포함된) tar.exe로 test.zip 파일을 풀어보면 이런 메시지가 하나 보입니다.

c:\temp> tar -xf test.zip
tar: Removing leading '/' from member names

그러니까, 아마도 윈도우 탐색기는 리눅스의 "/" 루트로 시작하는 절대 경로가 있는 파일이라면 정상적으로 해석을 못하는 것이 아닌가 싶은데요, 실제로 이 확인을 우리가 만든 소스 코드에서 "/" 루트 경로를 삭제하는 코드를 추가하면,

string filePath = entry.FullName.TrimStart('/');
ZipArchiveEntry newEntry = destArchive.CreateEntry(filePath);

새롭게 저장한 zip 파일은 탐색기로 잘 열립니다. 그렇다면 반대로, 탐색기에서 압축한 파일을 위의 소스 코드에서 루트 문자를 추가해 다시 저장하는 식으로 바꾸면,

string filePath = "/" + entry.FullName; // The Compressed (zipped) Folder '[...].zip' is invalid. 
ZipArchiveEntry newEntry = destArchive.CreateEntry(filePath);

탐색기로는 열리지 않는 zip 파일이 나옵니다. (이 글에 첨부한 invalid_sample.zip은 그렇게 만든 것입니다.)




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]







[최초 등록일: ]
[최종 수정일: 7/3/2023]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 



2023-07-10 10시11분
Tar and Curl Come to Windows
; https://learn.microsoft.com/en-us/virtualization/community/team-blog/2017/20171219-tar-and-curl-come-to-windows

wget의 경우 curl을 이용해 다음과 같은 식으로 우회해 해결할 수 있습니다.

curl --location [...url...] --output [...local_file_name...]

-----------

Added new Tar APIs
; https://devblogs.microsoft.com/dotnet/announcing-dotnet-7-preview-4/#added-new-tar-apis
정성태

1  2  3  4  5  6  7  [8]  9  10  11  12  13  14  15  ...
NoWriterDateCnt.TitleFile(s)
13432정성태10/31/20232455오류 유형: 878. 탐색기의 WSL 디렉터리 접근 시 "Attempt to access invalid address." 오류 발생
13431정성태10/31/20232790스크립트: 60. 파이썬 - 비동기 FastAPI 앱을 gunicorn으로 호스팅
13430정성태10/30/20232678닷넷: 2153. C# - 사용자가 빌드한 ICU dll 파일을 사용하는 방법
13429정성태10/27/20232961닷넷: 2152. Win32 Interop - C/C++ DLL로부터 이중 포인터 버퍼를 C#으로 받는 예제파일 다운로드1
13428정성태10/25/20233035닷넷: 2151. C# 12 - ref readonly 매개변수
13427정성태10/18/20233250닷넷: 2150. C# 12 - 정적 문맥에서 인스턴스 멤버에 대한 nameof 접근 허용(Allow nameof to always access instance members from static context)
13426정성태10/13/20233408스크립트: 59. 파이썬 - 비동기 호출 함수(run_until_complete, run_in_executor, create_task, run_in_threadpool)
13425정성태10/11/20233196닷넷: 2149. C# - PLinq의 Partitioner<T>를 이용한 사용자 정의 분할파일 다운로드1
13423정성태10/6/20233173스크립트: 58. 파이썬 - async/await 기본 사용법
13422정성태10/5/20233319닷넷: 2148. C# - async 유무에 따른 awaitable 메서드의 병렬 및 예외 처리
13421정성태10/4/20233395닷넷: 2147. C# - 비동기 메서드의 async 예약어 유무에 따른 차이
13420정성태9/26/20235585스크립트: 57. 파이썬 - UnboundLocalError: cannot access local variable '...' where it is not associated with a value
13419정성태9/25/20233220스크립트: 56. 파이썬 - RuntimeError: dictionary changed size during iteration
13418정성태9/25/20233925닷넷: 2146. C# - ConcurrentDictionary 자료 구조의 동기화 방식
13417정성태9/19/20233454닷넷: 2145. C# - 제네릭의 형식 매개변수에 속한 (매개변수를 가진) 생성자를 호출하는 방법
13416정성태9/19/20233259오류 유형: 877. redis-py - MISCONF Redis is configured to save RDB snapshots, ...
13415정성태9/18/20233756닷넷: 2144. C# 12 - 컬렉션 식(Collection Expressions)
13414정성태9/16/20233514디버깅 기술: 193. Windbg - ThreadStatic 필드 값을 조사하는 방법
13413정성태9/14/20233710닷넷: 2143. C# - 시스템 Time Zone 변경 시 이벤트 알림을 받는 방법
13412정성태9/14/20236992닷넷: 2142. C# 12 - 인라인 배열(Inline Arrays) [1]
13411정성태9/12/20233493Windows: 252. 권한 상승 전/후 따로 관리되는 공유 네트워크 드라이브 정보
13410정성태9/11/20235031닷넷: 2141. C# 12 - Interceptor (컴파일 시에 메서드 호출 재작성) [1]
13409정성태9/8/20233888닷넷: 2140. C# - Win32 API를 이용한 모니터 전원 끄기
13408정성태9/5/20233843Windows: 251. 임의로 만든 EXE 파일을 포함한 ZIP 파일의 압축을 해제할 때 Windows Defender에 의해 삭제되는 경우
13407정성태9/4/20233588닷넷: 2139. C# - ParallelEnumerable을 이용한 IEnumerable에 대한 병렬 처리
13406정성태9/4/20233557VS.NET IDE: 186. Visual Studio Community 버전의 라이선스
1  2  3  4  5  6  7  [8]  9  10  11  12  13  14  15  ...