รูปแบบ 7Z เป็นตัวเลือกที่นิยมสําหรับการบีบอัดและกระจายอัตราสูง ด้วย Aspose.ZIP สําหรับ .NET คุณสามารถสร้างไฟล์ 7z โปรแกรมโดยใช้ API ง่ายและทันสมัย คู่มือนี้ผ่านตัวอย่างทํางานขั้นต่ําและเพิ่มรูปแบบปฏิบัติสําหรับโครงการจริงเช่นการเพิ่มไฟล์จากดิสก์ข้อมูลจากหน่วยความจําการควบคุมชื่อเข้าและโฟลเดอร์ภายในไฟล์และการจัดการข้อผิดพลาดพื้นฐาน

ตัวอย่างรหัสทั้งหมดเป็นอินไลน์และมีเนื้อหาตนเอง

ข้อกําหนด

  • .NET 6 หรือเร็วกว่า
  • แพคเกจ NuGet Aspose.Zip
dotnet add package Aspose.Zip

ชื่อพื้นที่ที่ใช้ในตัวอย่าง:Aspose.Zip, Aspose.Zip.SevenZip

เริ่มต้นอย่างรวดเร็ว: สร้างไฟล์ 7z ด้วยไฟล์บางส่วน

ตัวอย่างขั้นต่ํานี้สร้างไฟล์ 7z ใหม่ในหน่วยความจําเพิ่มสองรายการและบันทึกไว้ในไดรฟ์

// File: Program.cs
using System;
using System.IO;
using System.Text;
using Aspose.Zip.SevenZip;

class Program
{
    static void Main()
    {
        var outputPath = "example.7z";

        using (var archive = new SevenZipArchive())
        {
            // Add a text file from memory
            using (var ms = new MemoryStream(Encoding.UTF8.GetBytes("Hello 7z from Aspose.ZIP")))
            {
                archive.CreateEntry("docs/readme.txt", ms);
            }

            // Add a file from disk
            var sourceFile = "report.pdf"; // ensure this file exists
            if (File.Exists(sourceFile))
            {
                using var fs = File.OpenRead(sourceFile);
                archive.CreateEntry("reports/2025/report.pdf", fs);
            }

            // Save the 7z archive
            using var outStream = File.Create(outputPath);
            archive.Save(outStream);
        }

        Console.WriteLine("Saved 7z: " + Path.GetFullPath(outputPath));
    }
}
  • สิ่งที่ต้องสังเกต *
  • SevenZipArchive เริ่มต้นที่ว่างเปล่าแล้วคุณเรียก CreateEntry(entryName, stream) ต่อรายการ
  • ชื่อป้อนตั้งค่าโครงสร้างโฟลเดอร์ภายในเอกสารเช่น docs/readme.txt.
  • คุณสามารถผ่านการอ่านใด ๆ Stream, ซึ่งมีประโยชน์สําหรับข้อมูลที่คุณสร้างในหน่วยความจํา

เพิ่มไม้โฟลเดอร์ทั้งหมด

เดินไดเรกทอรีอย่างต่อเนื่องรักษาโครงสร้าง relative ของมันภายในเอกสารและบันทึกเป็น .7z.

using System;
using System.IO;
using Aspose.Zip.SevenZip;

static class FolderTo7z
{
    public static void CreateFromFolder(string sourceDir, string output7z)
    {
        if (!Directory.Exists(sourceDir))
            throw new DirectoryNotFoundException(sourceDir);

        using var archive = new SevenZipArchive();

        var basePath = Path.GetFullPath(sourceDir);
        foreach (var filePath in Directory.GetFiles(basePath, "*", SearchOption.AllDirectories))
        {
            var relPath = Path.GetRelativePath(basePath, filePath)
                              .Replace(Path.DirectorySeparatorChar, '/'); // normalize to forward slashes

            using var fs = File.OpenRead(filePath);
            archive.CreateEntry(relPath, fs);
        }

        using var outStream = File.Create(output7z);
        archive.Save(outStream);
    }
}

// Usage
// FolderTo7z.CreateFromFolder(@"C:\data\input", "bundle.7z");
  • เคล็ดลับ *
  • ใช Path.GetRelativePath เพื่อให้การจัดวางโฟลเดอร์ภายในสะอาด
  • ปกติตัวแยกทิศทางเพื่อให้ล่วงหน้าสําหรับรายการไฟล์ที่สอดคล้อง

กระแสไฟล์ขนาดใหญ่ได้อย่างปลอดภัย

เมื่อเพิ่มไฟล์ขนาดใหญ่ให้สตรีมพวกเขาแทนที่จะบูฟเวอร์ไฟล์ทั้งหมดในหน่วยความจํา รูปแบบพื้นฐานด้านบนด้วย File.OpenRead มีการไหล หากคุณสร้างเนื้อหาบนเครื่องบินเขียนไว้ใน Stream และผ่านการไหลนี้โดยตรงไปยัง CreateEntry.

using System.IO;
using System.Text;
using Aspose.Zip.SevenZip;

static void AddGeneratedCsv(SevenZipArchive archive, string entryName)
{
    // Example generator that writes CSV rows in a forward-only fashion
    using var pipe = new MemoryStream();
    using var writer = new StreamWriter(pipe, Encoding.UTF8, leaveOpen: true);

    writer.WriteLine("id,name");
    for (int i = 1; i <= 1000; i++)
        writer.WriteLine($"{i},Item {i}");

    writer.Flush();
    pipe.Position = 0;

    archive.CreateEntry(entryName, pipe);
}

จัดการเข้าสู่ระบบด้วยผู้ช่วย

สําหรับงานขนาดใหญ่ใช้ผู้ช่วยขนาดเล็กเพื่อเพิ่มไฟล์ที่มีค่าเริ่มต้นทั่วไป สิ่งนี้ทําให้โครงสร้างเอกสารของคุณคาดการณ์ได้

using System.IO;
using Aspose.Zip.SevenZip;

static class SevenZipHelpers
{
    public static void AddFile(SevenZipArchive archive, string rootPrefix, string fullPath)
    {
        var relName = Path.Combine(rootPrefix, Path.GetFileName(fullPath))
                           .Replace(Path.DirectorySeparatorChar, '/');
        using var fs = File.OpenRead(fullPath);
        archive.CreateEntry(relName, fs);
    }
}

ตัวอย่างที่สิ้นสุดพร้อมการเข้าสู่ระบบและการตรวจสอบพื้นฐาน

ตัวอย่างนี้สร้างเอกสาร 7z จากรายการแหล่งผสม มันยืนยันเส้นทางและผลลัพธ์บันทึก

// File: BuildArchive.cs
using System;
using System.Collections.Generic;
using System.IO;
using Aspose.Zip.SevenZip;

public static class BuildArchive
{
    public static bool Run(IEnumerable<string> paths, string output7z)
    {
        if (paths == null) throw new ArgumentNullException(nameof(paths));
        Directory.CreateDirectory(Path.GetDirectoryName(Path.GetFullPath(output7z)) ?? ".");

        int added = 0, skipped = 0;

        using var archive = new SevenZipArchive();

        foreach (var p in paths)
        {
            if (string.IsNullOrWhiteSpace(p)) { skipped++; continue; }

            if (File.Exists(p))
            {
                using var fs = File.OpenRead(p);
                var entryName = Path.GetFileName(p);
                archive.CreateEntry(entryName, fs);
                Console.WriteLine("Added file: " + entryName);
                added++;
            }
            else if (Directory.Exists(p))
            {
                var basePath = Path.GetFullPath(p);
                foreach (var fp in Directory.GetFiles(basePath, "*", SearchOption.AllDirectories))
                {
                    var rel = Path.GetRelativePath(basePath, fp).Replace(Path.DirectorySeparatorChar, '/');
                    using var fs = File.OpenRead(fp);
                    archive.CreateEntry(rel, fs);
                    Console.WriteLine("Added: " + rel);
                    added++;
                }
            }
            else
            {
                Console.WriteLine("Skip missing: " + p);
                skipped++;
            }
        }

        using var outStream = File.Create(output7z);
        archive.Save(outStream);

        Console.WriteLine($"Saved: {Path.GetFullPath(output7z)}");
        Console.WriteLine($"Entries added: {added}, skipped: {skipped}");
        return added > 0;
    }
}

// Usage sample:
// BuildArchive.Run(new [] { "README.md", "assets", "docs/spec.pdf" }, "release.7z");

การปฏิบัติที่ดีที่สุด

  • ใช้ล่วงหน้าสําหรับชื่อเข้าสู่ระบบเครื่องมือหลายคนแสดงเส้นทางอย่างต่อเนื่องมากขึ้นเมื่อเข้าสู่ระบบใช้ / เช่นเดียวกับตัวแยก

  • ** มีการไหล *บันทึกไฟล์กระแสใน using บล็อก ตัวอย่างด้านบนให้แน่ใจว่าการจัดเก็บที่สะอาด

  • การเข้าสู่ระบบที่ถูกต้องตรวจสอบว่าไฟล์ที่มีอยู่ก่อนที่จะเพิ่มพวกเขา ให้บันทึกที่ชัดเจนสําหรับเส้นทางที่ผ่านไป

  • รักษาโครงสร้างเอกสารสะอาดเลือกชื่อโฟลเดอร์รากภายในเอกสารและติดกับมันเช่น app/ หรือ package/.

  • การทดสอบบนแพลตฟอร์มเป้าหมายตรวจสอบให้แน่ใจว่า 7z ของคุณเปิดอย่างถูกต้องในเครื่องมือที่ผู้ใช้ของคุณเชื่อมต่อ

คําอธิบาย

คุณสร้างไฟล์ 7z โปรแกรมด้วย Aspose.ZIP สําหรับ .NET เพิ่มไฟล์จากไดรฟ์และหน่วยความจํา حفظโครงสร้างโฟลเดอร์สะอาดและบันทึกผล .7zใช้รูปแบบเหล่านี้เพื่อบรรจุการปล่อยแพคเกจและส่งออกโดยตรงจากแอพ C# ของคุณ

More in this category