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)
13409정성태9/8/20233911닷넷: 2140. C# - Win32 API를 이용한 모니터 전원 끄기
13408정성태9/5/20233875Windows: 251. 임의로 만든 EXE 파일을 포함한 ZIP 파일의 압축을 해제할 때 Windows Defender에 의해 삭제되는 경우
13407정성태9/4/20233606닷넷: 2139. C# - ParallelEnumerable을 이용한 IEnumerable에 대한 병렬 처리
13406정성태9/4/20233587VS.NET IDE: 186. Visual Studio Community 버전의 라이선스
13405정성태9/3/20234013닷넷: 2138. C# - async 메서드 호출 원칙
13404정성태8/29/20233580오류 유형: 876. Windows - 키보드의 등호(=, Equals sign) 키가 눌리지 않는 경우
13403정성태8/21/20233376오류 유형: 875. The following signatures couldn't be verified because the public key is not available: NO_PUBKEY EB3E94ADBE1229CF
13402정성태8/20/20233465닷넷: 2137. ILSpy의 nuget 라이브러리 버전 - ICSharpCode.Decompiler
13401정성태8/19/20233704닷넷: 2136. .NET 5+ 환경에서 P/Invoke의 성능을 높이기 위한 SuppressGCTransition 특성 [1]
13400정성태8/10/20233535오류 유형: 874. 파이썬 - pymssql을 윈도우 환경에서 설치 불가
13399정성태8/9/20233491닷넷: 2135. C# - 지역 변수로 이해하는 메서드 매개변수의 값/참조 전달
13398정성태8/3/20234329스크립트: 55. 파이썬 - pyodbc를 이용한 SQL Server 연결 사용법
13397정성태7/23/20233821닷넷: 2134. C# - 문자열 연결 시 string.Create를 이용한 GC 할당 최소화
13396정성태7/22/20233583스크립트: 54. 파이썬 pystack 소개 - 메모리 덤프로부터 콜 스택 열거
13395정성태7/20/20233477개발 환경 구성: 685. 로컬에서 개발 중인 ASP.NET Core/5+ 웹 사이트에 대해 localhost 이외의 호스트 이름으로 접근하는 방법
13394정성태7/16/20233437오류 유형: 873. Oracle.ManagedDataAccess.Client - 쿼리 수행 시 System.InvalidOperationException
13393정성태7/16/20233610닷넷: 2133. C# - Oracle 데이터베이스의 Sleep 쿼리 실행하는 방법
13392정성태7/16/20233506오류 유형: 872. Oracle - ORA-01031: insufficient privileges
13391정성태7/14/20233542닷넷: 2132. C# - sealed 클래스의 메서드를 callback 호출했을 때 인라인 처리가 될까요?
13390정성태7/12/20233496스크립트: 53. 파이썬 - localhost 호출 시의 hang 현상
13389정성태7/5/20233527개발 환경 구성: 684. IIS Express로 호스팅하는 웹을 WSL 환경에서 접근하는 방법
13388정성태7/3/20233653오류 유형: 871. 윈도우 탐색기에서 열리지 않는 zip 파일 - The Compressed (zipped) Folder '[...].zip' is invalid. [1]파일 다운로드1
13387정성태6/28/20233721오류 유형: 870. _mysql - Commands out of sync; you can't run this command now
13386정성태6/27/20233778Linux: 61. docker - 원격 제어를 위한 TCP 바인딩 추가
13385정성태6/27/20233980Linux: 60. Linux - 외부에서의 접속을 허용하기 위한 TCP 포트 여는 방법
13384정성태6/26/20233707.NET Framework: 2131. C# - Source Generator로 해결하는 enum 박싱 문제파일 다운로드1
1  2  3  4  5  6  7  8  [9]  10  11  12  13  14  15  ...