Այս ձեռնարկում մենք ձեզ ցույց կտանք, թե ինչպես հանել Zip ֆայլը C# կոդով: Դուք կարող եք օգտագործել C#՝ ձեր հավելվածներում արխիվը բացելու համար: Այս կոդը աջակցում է մի քանի zip or archive file formats, ինչպիսիք են GZip, RAR, TAR, 7Zip և այլն:
Zip ֆայլը C#-ում հանելու քայլեր
- Տեղադրեք Aspose.Zip for .NET փաթեթը NuGet.org-ից
- Կոդում ներառեք Aspose.Zip անվանատարածք
- Aspose.Zip API-ի լիցենզիան կարգավորելու համար օգտագործեք SetLicense մեթոդը
- Ներբեռնեք մուտքագրված Zip ֆայլը FileStream օբյեկտի մեջ
- Ստեղծեք նոր Archive object ֆայլի հոսքից
- Ստացեք արխիվում գտնվող ֆայլերի քանակը և անցեք արխիվի գրառումների միջոցով
- Քաղեք արխիվի յուրաքանչյուր գրառում և պահեք ֆայլը սկավառակի վրա
Արխիվի յուրաքանչյուր գրառում պարունակում է ոչ միայն ֆայլ, այլև ֆայլի անվանումը: Մենք օգտագործել ենք Name հատկությունը՝ ֆայլի անունը ստանալու համար, այնուհետև հանել ենք նույն անունով ֆայլը:
Կոդ՝ Zip ֆայլը C#-ում հանելու համար
using System; | |
using System.IO; | |
using System.Text; | |
//Add reference to Aspose.Zip for .NET API | |
//Use following namespace to extract zip file | |
using Aspose.Zip; | |
namespace ExtractZipFile | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
//Set Aspose license before extracting Zip file | |
//using Aspose.Zip for .NET | |
Aspose.Zip.License AsposeZipLicense = new Aspose.Zip.License(); | |
AsposeZipLicense.SetLicense(@"c:\asposelicense\license.lic"); | |
//Open file from disk using a file stream | |
FileStream ZipFileToBeExtracted = File.Open("ZipFileToBeExtracted.zip", FileMode.Open); | |
//Load Zip file stream to Archive object | |
Archive ZipArchiveToExtract = new Archive(ZipFileToBeExtracted); | |
//Get number of files | |
int NumberOfFileInArchive = ZipArchiveToExtract.Entries.Count; | |
//Loop through the archive for each file | |
for(int FileCounter =0; FileCounter < NumberOfFileInArchive; FileCounter++) | |
{ | |
//Get each zip archive entry and extract the file | |
ArchiveEntry ArchiveFileEntry = ZipArchiveToExtract.Entries[FileCounter]; | |
string NameOfFileInZipEntry = ArchiveFileEntry.Name; | |
ArchiveFileEntry.Extract(NameOfFileInZipEntry); | |
} | |
} | |
} | |
} |
Վերոնշյալ կոդում մենք օգտագործել ենք FileStream՝ Zip archive-ը բեռնելու համար, այնուհետև պահել արդյունահանված ֆայլերը սկավառակի վրա: Կարող եք նաև օգտագործել այս կոդը C#-ում՝ հիշողության մեջ ֆայլը բացելու համար: Սա կարող է օգտակար լինել, երբ ձեզ հարկավոր են այդ ֆայլերը լրացուցիչ կոդում կամ հավելվածում և չեք ցանկանում պահել սկավառակի վրա: Օգտագործելով այս կոդը՝ դուք հեշտությամբ և արագ կարող եք ստեղծել ձեր սեփական C# Zip արդյունահանողը ձեր հավելվածներում կամ որպես անկախ կոմունալ: