Kako izvaditi Zip datoteku u C#
Uz Aspose.ZIP knjižnicu, možete lako unzip arhiva različitih formata kao što su ZIP, GZip, RAR, TAR i 7Zip izravno u vašim aplikacijama.
Prednosti ekstrakcije ZIP datoteke
Upravljanje datotekama:- Jednostavno upravlja i organizira velike skupine datoteka.
Kompatibilnost:- Jednostavno se integrira s različitim formatima datoteke koje podržava Aspose.ZIP.
Automatizirani postupci:- Idealan za aplikacije koje zahtijevaju automatizirane ekstrakcije datoteke.
Predviđanja: Priprema za okoliš
- Sastavite Visual Studio ili bilo koji kompatibilni .NET IDE.
- Instalirajte Aspose.ZIP od NuGet Package Manager.
Korak po korak vodič za uklanjanje Zip datoteke u C#
Korak 1: Instalirajte Aspose.ZIP
Počnite dodavanjem Aspose.ZIP knjižnice vašem projektu.
Install-Package Aspose.ZIP
Korak 2: Uključite nazivni prostor
Add a reference to the Aspose.Zip
namespace in your code.
using Aspose.Zip;
Korak 3: Preuzmite ZIP datoteku
Open the ZIP file using a FileStream
object.
FileStream zipFileToBeExtracted = File.Open("ZipFileToBeExtracted.zip", FileMode.Open);
Korak 4: Stvaranje arhivskog objekta
Load the FileStream
into an Archive object.
Archive zipArchiveToExtract = new Archive(zipFileToBeExtracted);
Korak 5: Izračunajte datoteke u arhivu
Povratak broja datoteka sadržanih u ZIP arhivu.
int numberOfFilesInArchive = zipArchiveToExtract.Entries.Count;
Korak 6: Izvadite svaki ulaz
Prođite kroz svaki ulaz u arhiv i izvadite datoteke.
for (int fileCounter = 0; fileCounter < numberOfFilesInArchive; fileCounter++)
{
ArchiveEntry archiveFileEntry = zipArchiveToExtract.Entries[fileCounter];
string nameOfFileInZipEntry = archiveFileEntry.Name;
archiveFileEntry.Extract(nameOfFileInZipEntry);
}
Kompletni primjer koda za uklanjanje ZIP datoteke
Sljedeći je potpuni primjer uklanjanja ZIP datoteke pomoću C#:
// Open file from disk using a file stream
FileStream zipFileToBeExtracted = File.Open("ZipFileToBeExtracted.zip", FileMode.Open);
// Load the Zip file stream into an Archive object
Archive zipArchiveToExtract = new Archive(zipFileToBeExtracted);
// Get the number of files in the archive
int numberOfFilesInArchive = zipArchiveToExtract.Entries.Count;
// Loop through the archive for each file
for (int fileCounter = 0; fileCounter < numberOfFilesInArchive; fileCounter++)
{
// Get each zip archive entry and extract the file
ArchiveEntry archiveFileEntry = zipArchiveToExtract.Entries[fileCounter];
string nameOfFileInZipEntry = archiveFileEntry.Name;
archiveFileEntry.Extract(nameOfFileInZipEntry);
}
Dodatne informacije
- Ova funkcija podržava ne samo ZIP datoteke, već i druge formate kao što su GZip, RAR i TAR.
- Također možete izvaditi datoteke izravno u memoriju ako je potrebno za daljnju obradu.
zaključak
Ovaj tutorial je pokazao kako izvaditi zip datoteke u C# pomoću Aspose.ZIP. Slijedom koraka i pomoću predviđenog primjera koda, možete lako integrirati zip ekstrakciju datoteke u svoje aplikacije.