Class Archive

Class Archive

Namespace: Aspose.Zip
Assembly: Aspose.Zip.dll (25.1.0)

คลาสนี้แทนไฟล์ zip archive ใช้เพื่อประกอบ, สกัด, หรืออัปเดต zip archives.

public class Archive : IArchive, IDisposable

การสืบทอด

objectArchive

การนำไปใช้

IArchive, IDisposable

สมาชิกที่สืบทอด

object.GetType(), object.MemberwiseClone(), object.ToString(), object.Equals(object?), object.Equals(object?, object?), object.ReferenceEquals(object?, object?), object.GetHashCode()

ตัวสร้าง

Archive(ArchiveEntrySettings)

สร้างอินสแตนซ์ใหม่ของคลาส Aspose.Zip.Archive พร้อมการตั้งค่าสำหรับรายการที่เลือกได้

public Archive(ArchiveEntrySettings newEntrySettings = null)

พารามิเตอร์

newEntrySettings ArchiveEntrySettings

การตั้งค่าการบีบอัดและการเข้ารหัสที่ใช้สำหรับรายการ Aspose.Zip.ArchiveEntry ที่เพิ่มใหม่ หากไม่ระบุ จะใช้การบีบอัด Deflate ที่พบบ่อยที่สุดโดยไม่มีการเข้ารหัส

ตัวอย่าง

ตัวอย่างต่อไปนี้แสดงวิธีการบีบอัดไฟล์เดียวด้วยการตั้งค่าเริ่มต้น

using (FileStream zipFile = File.Open("archive.zip", FileMode.Create))
{
    using (var archive = new Archive())
    {
        archive.CreateEntry("data.bin", "file.dat");
        archive.Save(zipFile);
    }
}

Archive(Stream, ArchiveLoadOptions, ArchiveEntrySettings)

สร้างอินสแตนซ์ใหม่ของคลาส Aspose.Zip.Archive และประกอบรายการที่สามารถสกัดจาก archive

public Archive(Stream sourceStream, ArchiveLoadOptions loadOptions = null, ArchiveEntrySettings newEntrySettings = null)

พารามิเตอร์

sourceStream Stream

แหล่งที่มาของ archive

loadOptions ArchiveLoadOptions

ตัวเลือกในการโหลด archive ที่มีอยู่

newEntrySettings ArchiveEntrySettings

การตั้งค่าการบีบอัดและการเข้ารหัสที่ใช้สำหรับรายการ Aspose.Zip.ArchiveEntry ที่เพิ่มใหม่ หากไม่ระบุ จะใช้การบีบอัด Deflate ที่พบบ่อยที่สุดโดยไม่มีการเข้ารหัส

ตัวอย่าง

ตัวอย่างต่อไปนี้สกัด archive ที่เข้ารหัส จากนั้นทำการบีบอัดรายการแรกไปยัง MemoryStream.

var fs = File.OpenRead("encrypted.zip");
var extracted = new MemoryStream();
using (Archive archive = new Archive(fs, new ArchiveLoadOptions() { DecryptionPassword = "p@s$" }))
{
    using (var decompressed = archive.Entries[0].Open())
    {
        byte[] b = new byte[8192];
        int bytesRead;
        while (0 < (bytesRead = decompressed.Read(b, 0, b.Length)))
            extracted.Write(b, 0, bytesRead);
    }
}

หมายเหตุ

ตัวสร้างนี้ไม่ทำการบีบอัดรายการใด ๆ ดูวิธีการ Aspose.Zip.ArchiveEntry.Open(System.String) สำหรับการบีบอัด

ข้อยกเว้น

ArgumentException

sourceStream ไม่สามารถค้นหาได้

InvalidDataException

หัวข้อการเข้ารหัสสำหรับ AES ขัดแย้งกับวิธีการบีบอัด WinZip

Archive(string, ArchiveLoadOptions, ArchiveEntrySettings)

สร้างอินสแตนซ์ใหม่ของคลาส Aspose.Zip.Archive และประกอบรายการที่สามารถสกัดจาก archive

public Archive(string path, ArchiveLoadOptions loadOptions = null, ArchiveEntrySettings newEntrySettings = null)

พารามิเตอร์

path string

เส้นทางที่มีคุณสมบัติครบถ้วนหรือเส้นทางสัมพัทธ์ไปยังไฟล์ archive

loadOptions ArchiveLoadOptions

ตัวเลือกในการโหลด archive ที่มีอยู่

newEntrySettings ArchiveEntrySettings

การตั้งค่าการบีบอัดและการเข้ารหัสที่ใช้สำหรับรายการ Aspose.Zip.ArchiveEntry ที่เพิ่มใหม่ หากไม่ระบุ จะใช้การบีบอัด Deflate ที่พบบ่อยที่สุดโดยไม่มีการเข้ารหัส

ตัวอย่าง

ตัวอย่างต่อไปนี้สกัด archive ที่เข้ารหัส จากนั้นทำการบีบอัดรายการแรกไปยัง MemoryStream.

var extracted = new MemoryStream();
using (Archive archive = new Archive("encrypted.zip", new ArchiveLoadOptions() { DecryptionPassword = "p@s$" }))
{
    using (var decompressed = archive.Entries[0].Open())
    {
        byte[] b = new byte[8192];
        int bytesRead;
        while (0 < (bytesRead = decompressed.Read(b, 0, b.Length)))
            extracted.Write(b, 0, bytesRead);
    }
}

หมายเหตุ

ตัวสร้างนี้ไม่ทำการบีบอัดรายการใด ๆ ดูวิธีการ Aspose.Zip.ArchiveEntry.Open(System.String) สำหรับการบีบอัด

ข้อยกเว้น

ArgumentNullException

path เป็น null

SecurityException

ผู้เรียกไม่มีสิทธิ์ที่จำเป็นในการเข้าถึง

ArgumentException

path ว่างเปล่า มีแต่ช่องว่าง หรือมีอักขระที่ไม่ถูกต้อง

UnauthorizedAccessException

การเข้าถึงไฟล์ path ถูกปฏิเสธ

PathTooLongException

path ที่ระบุ, ชื่อไฟล์, หรือทั้งสองอย่างเกินความยาวสูงสุดที่กำหนดโดยระบบ ตัวอย่างเช่น บนแพลตฟอร์ม Windows เส้นทางต้องมีความยาวน้อยกว่า 248 ตัวอักษร และชื่อไฟล์ต้องมีความยาวน้อยกว่า 260 ตัวอักษร

NotSupportedException

ไฟล์ที่ path มีเครื่องหมายโคลอน (:) อยู่กลางสตริง

FileNotFoundException

ไม่พบไฟล์

DirectoryNotFoundException

เส้นทางที่ระบุไม่ถูกต้อง เช่น อยู่บนไดรฟ์ที่ไม่ได้แมพ

IOException

ไฟล์เปิดอยู่แล้ว

InvalidDataException

ไฟล์เสียหาย

Archive(string, string[], ArchiveLoadOptions)

สร้างอินสแตนซ์ใหม่ของคลาส Aspose.Zip.Archive จาก multi-volume zip archive และประกอบรายการที่สามารถสกัดจาก archive

public Archive(string mainSegment, string[] segmentsInOrder, ArchiveLoadOptions loadOptions = null)

พารามิเตอร์

mainSegment string

เส้นทางไปยังส่วนสุดท้ายของ multi-volume archive ที่มี central directory

โดยปกติ ส่วนนี้จะมีนามสกุล *.zip และมีขนาดเล็กกว่าส่วนอื่น ๆ

segmentsInOrder string[]

เส้นทางไปยังแต่ละส่วนแต่ไม่ใช่ส่วนสุดท้ายของ multi-volume zip archive ตามลำดับ

โดยปกติพวกเขาจะตั้งชื่อ filename.z01, filename.z02, …, filename.z(n-1)

loadOptions ArchiveLoadOptions

ตัวเลือกในการโหลด archive ที่มีอยู่

ตัวอย่าง

ตัวอย่างนี้สกัดไปยังไดเรกทอรี archive ที่มีสามส่วน

using (Archive a = new Archive("archive.zip", new string[] { "archive.z01", "archive.z02" }))
{
    a.ExtractToDirectory("destination");
}

ข้อยกเว้น

EndOfStreamException

ไม่สามารถโหลด ZIP headers ได้เนื่องจากไฟล์ที่ให้มามีปัญหา

คุณสมบัติ

Entries

ดึงรายการประเภท Aspose.Zip.ArchiveEntry ที่ประกอบเป็น archive

public ReadOnlyCollection<archiveentry> Entries { get; }

ค่า Property

ReadOnlyCollection<ArchiveEntry&gt;

NewEntrySettings

การตั้งค่าการบีบอัดและการเข้ารหัสที่ใช้สำหรับรายการ Aspose.Zip.ArchiveEntry ที่เพิ่มใหม่

public ArchiveEntrySettings NewEntrySettings { get; }

ค่า Property

ArchiveEntrySettings

วิธีการ

CreateEntries(DirectoryInfo, bool)

เพิ่มไฟล์และไดเรกทอรีทั้งหมดลงใน archive โดยการค้นหาแบบ recursive ในไดเรกทอรีที่กำหนด

public Archive CreateEntries(DirectoryInfo directory, bool includeRootDirectory = true)

พารามิเตอร์

directory DirectoryInfo

ไดเรกทอรีที่จะบีบอัด

includeRootDirectory bool

ระบุว่าควรรวมไดเรกทอรีหลักเองหรือไม่

คืนค่า

Archive

archive ที่ประกอบด้วยรายการ

ตัวอย่าง

using (Archive archive = new Archive())
{
    DirectoryInfo folder = new DirectoryInfo("C:\folder");
    archive.CreateEntries(folder);
    archive.Save("folder.zip");
}

ข้อยกเว้น

DirectoryNotFoundException

เส้นทางไปยัง directory ไม่ถูกต้อง เช่น อยู่บนไดรฟ์ที่ไม่ได้แมพ

SecurityException

ผู้เรียกไม่มีสิทธิ์ที่จำเป็นในการเข้าถึง directory

CreateEntries(string, bool)

เพิ่มไฟล์และไดเรกทอรีทั้งหมดลงใน archive โดยการค้นหาแบบ recursive ในไดเรกทอรีที่กำหนด

public Archive CreateEntries(string sourceDirectory, bool includeRootDirectory = true)

พารามิเตอร์

sourceDirectory string

ไดเรกทอรีที่จะบีบอัด

includeRootDirectory bool

ระบุว่าควรรวมไดเรกทอรีหลักเองหรือไม่

คืนค่า

Archive

archive ที่ประกอบด้วยรายการ

ตัวอย่าง

using (Archive archive = new Archive())
{
    archive.CreateEntries("C:\folder");
    archive.Save("folder.zip");
}

CreateEntry(string, string, bool, ArchiveEntrySettings)

สร้างรายการเดียวภายใน archive

public ArchiveEntry CreateEntry(string name, string path, bool openImmediately = false, ArchiveEntrySettings newEntrySettings = null)

พารามิเตอร์

name string

ชื่อของรายการ

path string

ชื่อไฟล์ที่มีคุณสมบัติครบถ้วนใหม่ หรือชื่อไฟล์สัมพัทธ์ที่จะบีบอัด

openImmediately bool

เป็นจริงหากเปิดไฟล์ทันที มิฉะนั้นจะเปิดไฟล์เมื่อบันทึก archive

newEntrySettings ArchiveEntrySettings

การตั้งค่าการบีบอัดและการเข้ารหัสที่ใช้สำหรับรายการ Aspose.Zip.ArchiveEntry ที่เพิ่ม

คืนค่า

ArchiveEntry

อินสแตนซ์ zip entry

ตัวอย่าง

using (FileStream zipFile = File.Open("archive.zip", FileMode.Create))
{
    using (var archive = new Archive())
    {
        archive.CreateEntry("data.bin", "file.dat");
        archive.Save(zipFile);
    }
}

หมายเหตุ

ชื่อรายการจะถูกตั้งค่าเฉพาะภายในพารามิเตอร์ name ชื่อไฟล์ที่ให้ในพารามิเตอร์ path จะไม่มีผลต่อชื่อรายการ

หากไฟล์ถูกเปิดทันทีด้วยพารามิเตอร์ openImmediately จะถูกบล็อกจนกว่าจะบันทึก archive

ข้อยกเว้น

ArgumentNullException

path เป็น null

SecurityException

ผู้เรียกไม่มีสิทธิ์ที่จำเป็นในการเข้าถึง

ArgumentException

path ว่างเปล่า มีแต่ช่องว่าง หรือมีอักขระที่ไม่ถูกต้อง

UnauthorizedAccessException

การเข้าถึงไฟล์ path ถูกปฏิเสธ

PathTooLongException

path ที่ระบุ, ชื่อไฟล์, หรือทั้งสองอย่างเกินความยาวสูงสุดที่กำหนดโดยระบบ ตัวอย่างเช่น บนแพลตฟอร์ม Windows เส้นทางต้องมีความยาวน้อยกว่า 248 ตัวอักษร และชื่อไฟล์ต้องมีความยาวน้อยกว่า 260 ตัวอักษร

NotSupportedException

ไฟล์ที่ path มีเครื่องหมายโคลอน (:) อยู่กลางสตริง

CreateEntry(string, Stream, ArchiveEntrySettings)

สร้างรายการเดียวภายใน archive

public ArchiveEntry CreateEntry(string name, Stream source, ArchiveEntrySettings newEntrySettings = null)

พารามิเตอร์

name string

ชื่อของรายการ

source Stream

สตรีมข้อมูลนำเข้าของรายการ

newEntrySettings ArchiveEntrySettings

การตั้งค่าการบีบอัดและการเข้ารหัสที่ใช้สำหรับรายการ Aspose.Zip.ArchiveEntry ที่เพิ่ม

คืนค่า

ArchiveEntry

อินสแตนซ์ zip entry

ตัวอย่าง

using (var archive = new Archive(new ArchiveEntrySettings(null, new AesEcryptionSettings("p@s$", EncryptionMethod.AES256))))
{
    archive.CreateEntry("data.bin", new MemoryStream(new byte[] {0x00, 0xFF} ));
    archive.Save("archive.zip");
}

CreateEntry(string, FileInfo, bool, ArchiveEntrySettings)

สร้างรายการเดียวภายใน archive

public ArchiveEntry CreateEntry(string name, FileInfo fileInfo, bool openImmediately = false, ArchiveEntrySettings newEntrySettings = null)

พารามิเตอร์

name string

ชื่อของรายการ

fileInfo FileInfo

ข้อมูลเมตาของไฟล์ที่จะบีบอัด

openImmediately bool

เป็นจริงหากเปิดไฟล์ทันที มิฉะนั้นจะเปิดไฟล์เมื่อบันทึก archive

newEntrySettings ArchiveEntrySettings

การตั้งค่าการบีบอัดและการเข้ารหัสที่ใช้สำหรับรายการ Aspose.Zip.ArchiveEntry ที่เพิ่ม

คืนค่า

ArchiveEntry

อินสแตนซ์ zip entry

ตัวอย่าง

ประกอบ archive ด้วยรายการที่เข้ารหัสด้วยวิธีการเข้ารหัสและรหัสผ่านที่แตกต่างกันแต่ละอัน

using (FileStream zipFile = File.Open("archive.zip", FileMode.Create))
{
    FileInfo fi1 = new FileInfo("data1.bin");
    FileInfo fi2 = new FileInfo("data2.bin");
    FileInfo fi3 = new FileInfo("data3.bin");
    using (var archive = new Archive())
    {
        archive.CreateEntry("entry1.bin", fi1, false, new ArchiveEntrySettings(new DeflateCompressionSettings(), new TraditionalEncryptionSettings("pass1")));
        archive.CreateEntry("entry2.bin", fi2, false, new ArchiveEntrySettings(new DeflateCompressionSettings(), new AesEcryptionSettings("pass2", EncryptionMethod.AES128)));
        archive.CreateEntry("entry3.bin", fi3, false, new ArchiveEntrySettings(new DeflateCompressionSettings(), new AesEcryptionSettings("pass3", EncryptionMethod.AES256)));
        archive.Save(zipFile);
    }
}

หมายเหตุ

ชื่อรายการจะถูกตั้งค่าเฉพาะภายในพารามิเตอร์ name ชื่อไฟล์ที่ให้ในพารามิเตอร์ fileInfo จะไม่มีผลต่อชื่อรายการ

หากไฟล์ถูกเปิดทันทีด้วยพารามิเตอร์ openImmediately จะถูกบล็อกจนกว่าจะบันทึก archive

ข้อยกเว้น

UnauthorizedAccessException

fileInfo เป็นแบบอ่านอย่างเดียวหรือเป็นไดเรกทอรี

DirectoryNotFoundException

เส้นทางที่ระบุไม่ถูกต้อง เช่น อยู่บนไดรฟ์ที่ไม่ได้แมพ

IOException

ไฟล์เปิดอยู่แล้ว

CreateEntry(string, Stream, ArchiveEntrySettings, FileSystemInfo)

สร้างรายการเดียวภายใน archive

public ArchiveEntry CreateEntry(string name, Stream source, ArchiveEntrySettings newEntrySettings, FileSystemInfo fileInfo)

พารามิเตอร์

name string

ชื่อของรายการ

source Stream

สตรีมข้อมูลนำเข้าของรายการ

newEntrySettings ArchiveEntrySettings

การตั้งค่าการบีบอัดและการเข้ารหัสที่ใช้สำหรับรายการ Aspose.Zip.ArchiveEntry ที่เพิ่ม

fileInfo FileSystemInfo

ข้อมูลเมตาของไฟล์หรือโฟลเดอร์ที่จะบีบอัด

คืนค่า

ArchiveEntry

อินสแตนซ์ zip entry

ตัวอย่าง

ประกอบ archive ด้วยรายการที่เข้ารหัส

using (FileStream zipFile = File.Open("archive.zip", FileMode.Create))
{
    using (var archive = new Archive())
    {
        archive.CreateEntry("entry1.bin", new MemoryStream(new byte[] {0x00, 0xFF} ), new ArchiveEntrySettings(new DeflateCompressionSettings(), new TraditionalEncryptionSettings("pass1")), new FileInfo("data1.bin")); 
        archive.Save(zipFile);
    }
}

หมายเหตุ

ชื่อรายการจะถูกตั้งค่าเฉพาะภายในพารามิเตอร์ name ชื่อไฟล์ที่ให้ในพารามิเตอร์ fileInfo จะไม่มีผลต่อชื่อรายการ

fileInfo สามารถอ้างถึง System.IO.DirectoryInfo หากรายการเป็นไดเรกทอรี

ข้อยกเว้น

InvalidOperationException

ทั้ง source และ fileInfo เป็น null หรือ source เป็น null และ fileInfo แทนที่เป็นไดเรกทอรี

CreateEntry(string, Func<stream>, ArchiveEntrySettings)

สร้างรายการเดียวภายใน archive

public ArchiveEntry CreateEntry(string name, Func<stream> streamProvider, ArchiveEntrySettings newEntrySettings = null)

พารามิเตอร์

name string

ชื่อของรายการ

streamProvider Func<Stream&gt;

วิธีการที่ให้สตรีมข้อมูลนำเข้าสำหรับรายการ

newEntrySettings ArchiveEntrySettings

การตั้งค่าการบีบอัดและการเข้ารหัสที่ใช้สำหรับรายการ Aspose.Zip.ArchiveEntry ที่เพิ่ม

คืนค่า

ArchiveEntry

อินสแตนซ์ zip entry

ตัวอย่าง

ประกอบ archive ด้วยรายการที่เข้ารหัส

System.Func&lt;Stream&gt; provider = delegate(){ return new MemoryStream(new byte[]{0xFF, 0x00}); };
using (FileStream zipFile = File.Open("archive.zip", FileMode.Create))
{
    using (var archive = new Archive())
    {
        archive.CreateEntry("entry1.bin", provider, new ArchiveEntrySettings(new DeflateCompressionSettings(), new TraditionalEncryptionSettings("pass1")))); 
        archive.Save(zipFile);
    }
}

หมายเหตุ

วิธีการนี้ใช้สำหรับ .NET Framework 4.0 ขึ้นไปและสำหรับ .NET Standard 2.0

DeleteEntry(ArchiveEntry)

ลบการเกิดขึ้นแรกของรายการเฉพาะจากรายการรายการ

public Archive DeleteEntry(ArchiveEntry entry)

พารามิเตอร์

entry ArchiveEntry

รายการที่จะลบจากรายการรายการ

คืนค่า

Archive

archive ที่มีรายการถูกลบ

ตัวอย่าง

นี่คือวิธีที่คุณสามารถลบรายการทั้งหมดยกเว้นรายการสุดท้าย:

using (var archive = new Archive("archive.zip"))
{
    while (archive.Entries.Count &gt; 1)
        archive.DeleteEntry(archive.Entries[0]);
    archive.Save("last_entry.zip");
}

ข้อยกเว้น

ObjectDisposedException

archive ถูกปล่อยแล้ว

DeleteEntry(int)

ลบรายการจากรายการรายการตามดัชนี

public Archive DeleteEntry(int entryIndex)

พารามิเตอร์

entryIndex int

ดัชนีที่เริ่มต้นที่เป็นศูนย์ของรายการที่จะลบ

คืนค่า

Archive

archive ที่มีรายการถูกลบ

ตัวอย่าง

using (var archive = new TarArchive("two_files.zip"))
{
    archive.DeleteEntry(0);
    archive.Save("single_file.zip");
}

ข้อยกเว้น

ObjectDisposedException

archive ถูกปล่อยแล้ว

ArgumentOutOfRangeException

entryIndex น้อยกว่า 0.-หรือ- entryIndex เท่ากับหรือมากกว่า Entries จำนวน

Dispose()

ดำเนินการตามที่กำหนดโดยแอปพลิเคชันที่เกี่ยวข้องกับการปล่อย, การปล่อย, หรือการรีเซ็ตทรัพยากรที่ไม่จัดการ

public void Dispose()

Dispose(bool)

ดำเนินการตามที่กำหนดโดยแอปพลิเคชันที่เกี่ยวข้องกับการปล่อย, การปล่อย, หรือการรีเซ็ตทรัพยากรที่ไม่จัดการ

protected virtual void Dispose(bool disposing)

พารามิเตอร์

disposing bool

ระบุว่าทรัพยากรที่จัดการควรถูกปล่อยหรือไม่

ExtractToDirectory(string)

สกัดไฟล์ทั้งหมดใน archive ไปยังไดเรกทอรีที่ให้ไว้

public void ExtractToDirectory(string destinationDirectory)

พารามิเตอร์

destinationDirectory string

เส้นทางไปยังไดเรกทอรีที่จะวางไฟล์ที่ถูกสกัด

ตัวอย่าง

using (var archive = new Archive("archive.zip")) 
{ 
   archive.ExtractToDirectory("C:\extracted");
}

หมายเหตุ

หากไดเรกทอรีไม่อยู่ จะถูกสร้างขึ้น

ข้อยกเว้น

ArgumentNullException

destinationDirectory เป็น null

PathTooLongException

เส้นทาง, ชื่อไฟล์, หรือทั้งสองอย่างเกินความยาวสูงสุดที่กำหนดโดยระบบ ตัวอย่างเช่น บนแพลตฟอร์ม Windows เส้นทางต้องมีความยาวน้อยกว่า 248 ตัวอักษรและชื่อไฟล์ต้องมีความยาวน้อยกว่า 260 ตัวอักษร

SecurityException

ผู้เรียกไม่มีสิทธิ์ที่จำเป็นในการเข้าถึงไดเรกทอรีที่มีอยู่

NotSupportedException

หากไดเรกทอรีไม่มีอยู่ เส้นทางมีเครื่องหมายโคลอน (:) ที่ไม่ใช่ส่วนหนึ่งของป้ายไดรฟ์ (“C:")

ArgumentException

destinationDirectory เป็นสตริงที่มีความยาวศูนย์ มีแต่ช่องว่าง หรือมีอักขระที่ไม่ถูกต้องหนึ่งหรือมากกว่า คุณสามารถสอบถามหาตัวอักษรที่ไม่ถูกต้องโดยใช้วิธีการ System.IO.Path.GetInvalidPathChars -หรือ- เส้นทางมีเครื่องหมายโคลอน (:)

IOException

ไดเรกทอรีที่ระบุโดยเส้นทางเป็นไฟล์ - หรือ - ชื่อเครือข่ายไม่เป็นที่รู้จัก

InvalidDataException

รหัสผ่านที่ให้มีความไม่ถูกต้อง - หรือ - archive เสียหาย

Save(Stream, ArchiveSaveOptions)

บันทึก archive ไปยังสตรีมที่ให้ไว้

public void Save(Stream outputStream, ArchiveSaveOptions saveOptions = null)

พารามิเตอร์

outputStream Stream

สตรีมปลายทาง

saveOptions ArchiveSaveOptions

ตัวเลือกสำหรับการบันทึก archive

ตัวอย่าง

using (FileStream zipFile = File.Open("archive.zip", FileMode.Create))
{
    using (var archive = new Archive())
    {
        archive.CreateEntry("entry.bin", "data.bin");
        archive.Save(zipFile);
    }
}

หมายเหตุ

outputStream ต้องสามารถเขียนได้

ข้อยกเว้น

ArgumentException

outputStream ไม่สามารถเขียนได้

ObjectDisposedException

archive ถูกปล่อยแล้ว

Save(string, ArchiveSaveOptions)

บันทึก archive ไปยังไฟล์ปลายทางที่ให้ไว้

public void Save(string destinationFileName, ArchiveSaveOptions saveOptions = null)

พารามิเตอร์

destinationFileName string

เส้นทางของ archive ที่จะสร้าง หากชื่อไฟล์ที่ระบุชี้ไปยังไฟล์ที่มีอยู่ จะถูกเขียนทับ

saveOptions ArchiveSaveOptions

ตัวเลือกสำหรับการบันทึก archive

ตัวอย่าง

using (var archive = new Archive())
{
    archive.CreateEntry("entry.bin", "data.bin");
    archive.Save("archive.zip",  new ArchiveSaveOptions() { Encoding = Encoding.ASCII });
}

หมายเหตุ

สามารถบันทึก archive ไปยังเส้นทางเดียวกับที่โหลดมาจาก อย่างไรก็ตาม ไม่แนะนำให้ใช้วิธีนี้เพราะวิธีนี้ใช้การคัดลอกไปยังไฟล์ชั่วคราว

ข้อยกเว้น

ArgumentNullException

destinationFileName เป็น null

SecurityException

ผู้เรียกไม่มีสิทธิ์ที่จำเป็นในการเข้าถึง

ArgumentException

destinationFileName ว่างเปล่า มีแต่ช่องว่าง หรือมีอักขระที่ไม่ถูกต้อง

UnauthorizedAccessException

การเข้าถึงไฟล์ destinationFileName ถูกปฏิเสธ

PathTooLongException

destinationFileName ที่ระบุ, ชื่อไฟล์, หรือทั้งสองอย่างเกินความยาวสูงสุดที่กำหนดโดยระบบ ตัวอย่างเช่น บนแพลตฟอร์ม Windows เส้นทางต้องมีความยาวน้อยกว่า 248 ตัวอักษร และชื่อไฟล์ต้องมีความยาวน้อยกว่า 260 ตัวอักษร

NotSupportedException

ไฟล์ที่ destinationFileName มีเครื่องหมายโคลอน (:) อยู่กลางสตริง

FileNotFoundException

ไม่พบไฟล์

DirectoryNotFoundException

เส้นทางที่ระบุไม่ถูกต้อง เช่น อยู่บนไดรฟ์ที่ไม่ได้แมพ

IOException

ไฟล์เปิดอยู่แล้ว

SaveSplit(string, SplitArchiveSaveOptions)

บันทึก multi-volume archive ไปยังไดเรกทอรีปลายทางที่ให้ไว้

public void SaveSplit(string destinationDirectory, SplitArchiveSaveOptions options)

พารามิเตอร์

destinationDirectory string

เส้นทางไปยังไดเรกทอรีที่ archive segments จะถูกสร้าง

options SplitArchiveSaveOptions

ตัวเลือกสำหรับการบันทึก archive รวมถึงชื่อไฟล์

ตัวอย่าง

using (Archive archive = new Archive())
{
    archive.CreateEntry("entry.bin", "data.bin");
    archive.SaveSplit(@"C:\Folder",  new SplitArchiveSaveOptions("volume", 65536));
}

หมายเหตุ

วิธีนี้ประกอบด้วยหลายไฟล์ (n) ชื่อไฟล์ filename.z01, filename.z02, ..., filename.z(n-1), filename.zip

ไม่สามารถทำให้ archive ที่มีอยู่เป็น multi-volume ได้

ข้อยกเว้น

InvalidOperationException

archive นี้ถูกเปิดจากแหล่งที่มีอยู่

NotSupportedException

archive นี้ถูกบีบอัดด้วยวิธี XZ และเข้ารหัส

ArgumentNullException

destinationDirectory เป็น null

SecurityException

ผู้เรียกไม่มีสิทธิ์ที่จำเป็นในการเข้าถึงไดเรกทอรี

ArgumentException

destinationDirectory มีอักขระที่ไม่ถูกต้อง เช่น “, >, <, หรือ |

PathTooLongException

เส้นทางที่ระบุเกินความยาวสูงสุดที่กำหนดโดยระบบ

ObjectDisposedException

archive ถูกปล่อยแล้ว

 แบบไทย