소개

Zip 파일은 데이터 구성 및 압축에 필수적이지만, 프로그래밍 방식으로 내용을 어떻게 수정할 수 있을까요? 해결책은 C#을 사용하여 Zip 파일을 간편하게 조작할 수 있는 강력한 라이브러리인 Aspose.Zip for .NET을 사용하는 것입니다. 이 튜토리얼에서는 Zip 파일의 압축을 풀고, 삭제하고, 항목을 추가하는 방법을 단계별로 안내합니다.

필수 조건

자세히 알아보기 전에 다음 사항을 확인하세요.

  1. Aspose.Zip for .NET 라이브러리: 프로젝트에 라이브러리를 설치하세요. 다운로드할 수 있습니다. 여기.

  2. 문서 디렉터리: zip 파일을 저장할 디렉터리를 설정합니다. 바꾸기 "Your Document Directory" 실제 경로를 코드에 넣으세요.

필요한 네임스페이스 가져오기

먼저 필요한 네임스페이스를 가져옵니다.

using Aspose.Zip;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

1단계: 외부 Zip 파일 열기

먼저, 기본 zip 파일(외부 zip)을 엽니다.

string dataDir = "Your Data Directory";
using (Archive outer = new Archive(dataDir + "outer.zip"))
{
    // 내부 우편번호 항목을 식별하세요
}

2단계: 내부 우편번호 항목 식별

다음으로, 내부 zip 파일을 식별하고 삭제할 준비를 합니다.

List<ArchiveEntry> entriesToDelete = new List<ArchiveEntry>();
List<string> namesToInsert = new List<string>();
List<MemoryStream> contentToInsert = new List<MemoryStream>();

foreach (ArchiveEntry entry in outer.Entries)
{
    if (entry.Name.EndsWith(".zip", StringComparison.InvariantCultureIgnoreCase))
    {
        entriesToDelete.Add(entry);
        
        using (MemoryStream innerCompressed = new MemoryStream())
        {
            entry.Open().CopyTo(innerCompressed);
            
            // 내부 항목 추출
            using (Archive inner = new Archive(innerCompressed))
            {
                foreach (ArchiveEntry ie in inner.Entries)
                {
                    namesToInsert.Add(ie.Name);
                    MemoryStream content = new MemoryStream();
                    ie.Open().CopyTo(content);
                    contentToInsert.Add(content);
                }
            }
        }
    }
}

3단계: 내부 보관 항목 삭제

필요한 항목을 모은 후 내부 zip 항목을 삭제하세요.

foreach (ArchiveEntry e in entriesToDelete)
{
    outer.DeleteEntry(e);
}

4단계: 외부 Zip에 수정된 항목 추가

이제 새로 추출한 항목을 외부 zip 파일에 다시 추가할 수 있습니다.

for (int i = 0; i < namesToInsert.Count; i++)
{
    outer.CreateEntry(namesToInsert[i], contentToInsert[i]);
}

5단계: 수정된 Zip 파일 저장

마지막으로, 변경 사항을 새 zip 파일에 저장합니다.

outer.Save(dataDir + "flatten.zip");

결론

Aspose.Zip for .NET은 zip 파일을 프로그래밍 방식으로 조작하는 강력하고 간편한 방법을 제공합니다. 이 튜토리얼에서는 zip 파일의 압축 해제, 삭제, 항목 추가를 다루며 라이브러리의 다재다능함을 보여주었습니다. 다양한 시나리오를 살펴보고 파일 조작 기술을 향상시켜 보세요!

자주 묻는 질문

Aspose.Zip for .NET을 다른 프로그래밍 언어와 함께 사용할 수 있나요?

Aspose.Zip은 주로 .NET 애플리케이션용으로 설계되었지만 Aspose는 다양한 프로그래밍 언어에 대해 유사한 라이브러리를 제공합니다.

Aspose.Zip for .NET에 대한 무료 평가판이 있나요?

네, 무료 체험판을 다운로드할 수 있습니다. 여기.

.NET용 Aspose.Zip에 대한 지원은 어떻게 받을 수 있나요?

방문하세요 Aspose.Zip 포럼 지원과 토론을 위해.

Aspose.Zip for .NET에 대한 임시 라이선스를 구매할 수 있나요?

네, 임시면허를 취득할 수 있습니다. 여기.

.NET용 Aspose.Zip에 대한 문서는 어디에서 찾을 수 있나요?

전체 문서를 사용할 수 있습니다 여기.