programing tip

“System.IO.Compression”네임 스페이스에서“ZipFile”클래스를 찾지 못했습니다

itbloger 2020. 7. 30. 10:05
반응형

“System.IO.Compression”네임 스페이스에서“ZipFile”클래스를 찾지 못했습니다


네임 스페이스 "System.IO.Compression"에서 "Zipfile"클래스를 사용할 수 없습니다. 내 코드는 다음과 같습니다.

using System;
using System.IO;
using System.IO.Compression;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string startPath = @"c:\example\start";
            string zipPath = @"c:\example\result.zip";
            string extractPath = @"c:\example\extract";

            ZipFile.CreateFromDirectory(startPath, zipPath, CompressionLevel.Fastest,true);

            ZipFile.ExtractToDirectory(zipPath, extractPath);
        }
    }
}

오류는 다음과 같습니다

현재 컨텍스트에 'zipfile'이라는 이름이 없습니다.

어떻게 해결할 수 있습니까?


"System.IO.Compression.FileSystem.dll"어셈블리에 대한 dll 참조를 추가하고 .NET 4.5를 사용하고 있는지 확인하십시오 (이전 프레임 워크에는 없기 때문에).

자세한 내용 은 MSDN에서 어셈블리 및 .NET 버전을 찾을 수 있습니다.


MarcGravell이 지적한 대로 DLL 참조를 추가하려면 .NET의 친환경 프로그래머 인 경우 다음 단계를 수행하십시오.

Visual C #에서 참조를 추가하려면

  1. 솔루션 탐색기에서 프로젝트 노드를 마우스 오른쪽 단추로 클릭하고 참조 추가를 클릭하십시오.
  2. 참조 추가 대화 상자에서 참조 할 구성 요소 유형을 나타내는 탭을 선택하십시오.
  3. 참조하려는 구성 요소를 선택한 다음 확인을 클릭하십시오.

MSDN 문서의 방법 : 참조 추가 대화 상자를 사용하여 참조 추가 또는 제거 .


4.5로 업그레이드 할 수없는 경우 외부 패키지를 사용할 수 있습니다. 그중 하나는 DotNetZipLib의 Ionic.Zip.dll입니다.

using Ionic.Zip;

여기에서 무료로 다운로드 할 수 있습니다. http://dotnetzip.codeplex.com/


참조로 이동하여 "System.IO.Compression.FileSystem"을 추가하십시오.


나는 이것이 오래된 스레드라는 것을 알고 있지만, 이것에 대한 유용한 정보를 게시하는 것을 피할 수는 없습니다. Zip 질문이 많이 나오는 것을 보았습니다. 이것은 거의 대부분의 일반적인 질문에 대답합니다.

4.5+ 사용의 프레임 워크 문제를 해결하기 위해 jaime-olivares가 만든 ZipStorer 클래스입니다. https://github.com/jaime-olivares/zipstorer , 그는이 클래스를 다음과 같이 사용하는 방법에 대한 예제를 추가했습니다. 또한 특정 파일 이름을 검색하는 방법에 대한 예제도 추가했습니다.

그리고 이것을 사용하고 특정 파일 확장자를 반복하는 방법에 대한 참조를 위해 다음과 같이 할 수 있습니다.

#region
/// <summary>
/// Custom Method - Check if 'string' has '.png' or '.PNG' extension.
/// </summary>
static bool HasPNGExtension(string filename)
{
    return Path.GetExtension(filename).Equals(".png", StringComparison.InvariantCultureIgnoreCase)
        || Path.GetExtension(filename).Equals(".PNG", StringComparison.InvariantCultureIgnoreCase);
}
#endregion

private void button1_Click(object sender, EventArgs e)
{
    //NOTE: I recommend you add path checking first here, added the below as example ONLY.
    string ZIPfileLocationHere = @"C:\Users\Name\Desktop\test.zip";
    string EXTRACTIONLocationHere = @"C:\Users\Name\Desktop";

    //Opens existing zip file.
    ZipStorer zip = ZipStorer.Open(ZIPfileLocationHere, FileAccess.Read);

    //Read all directory contents.
    List<ZipStorer.ZipFileEntry> dir = zip.ReadCentralDir();

    foreach (ZipStorer.ZipFileEntry entry in dir)
    {
        try
        {
            //If the files in the zip are "*.png or *.PNG" extract them.
            string path = Path.Combine(EXTRACTIONLocationHere, (entry.FilenameInZip));
            if (HasPNGExtension(path))
            {
                //Extract the file.
                zip.ExtractFile(entry, path);
            }
        }
        catch (InvalidDataException)
        {
            MessageBox.Show("Error: The ZIP file is invalid or corrupted");
            continue;
        }
        catch
        {
            MessageBox.Show("Error: An unknown error ocurred while processing the ZIP file.");
            continue;
        }
    }
    zip.Close();
}

도움이 된 솔루션 : 도구> NuGet 패키지 관리자> 솔루션 용 NuGet 패키지 관리 ...> 찾아보기> System.IO.Compression.ZipFile 검색으로 이동하여 설치하십시오.


솔루션 탐색기에서 참조를 마우스 오른쪽 단추로 클릭 한 다음을 클릭하여 어셈블리를 펼치고 System.IO.Compression.FileSystem을 찾아서 확인하십시오. 그런 다음 수업에서 사용할 수 있습니다.using System.IO.Compression;

참조 어셈블리 추가 스크린 샷


System.IO.Compression is now available as a nuget package maintained by Microsoft.

To use ZipFile you need to download System.IO.Compression.ZipFile nuget package.


The issue here is that you just Added the reference to System.IO.Compression it is missing the reference to System.IO.Compression.Filesystem.dll

And you need to do it on .net 4.5 or later (because it doesn't exist on older versions).

I just posted a script on TechNet Maybe somebody would find it useful it requires .net 4.5 or 4.7

https://gallery.technet.microsoft.com/scriptcenter/Create-a-Zip-file-from-a-b23a7530


Add System.IO.Compression.ZipFile as nuget reference it is working

참고URL : https://stackoverflow.com/questions/15241889/i-didnt-find-zipfile-class-in-the-system-io-compression-namespace

반응형