Bu adım adım öğreticide, C# ile bellekte ZIP dosyasının nasıl oluşturulacağını öğreneceksiniz. Bellekte ZIP arşivi oluşturabilmek için aşağıdaki basit adımları uygulamanız gerekir.
C# ile Bellekte ZIP Dosyası Oluşturma Adımları
- Çözüme Aspose.ZIP for .NET NuGet paket referansı ekleyin
- Program.cs kod dosyasına Aspose.Zip deyimini kullanarak ekleyin
- License.SetLicense yöntemini kullanarak Aspose.ZIP API için lisans uygulayın
- Diskte bulunan Klasörden tek veya tüm Dosyaları alın ve yollarını dize dizisinde saklayın
- Bellekte Archive Class nesnesi oluşturun
- Her dosya için ZIP Arşivi’nde bir ArchiveEntry düğümü oluşturun
- ZIP Arşivi örneğini MemoryStream’e kaydedin veya kopyalayın
Aşağıdaki C# kodu örneği, diskteki bir klasördeki tüm dosyaları ZIP file format’e sıkıştırmak ve ardından ZIP dosyasını bellek akış nesnesinde saklamak için .NET uygulamanızda kullanılabilir.
C# ile Bellekte ZIP Dosyası Oluşturma Kodu
using System.IO; | |
// Following namespace is required to Create ZIP File in Memory | |
using Aspose.Zip; | |
namespace CreateZipFileInMemory | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
License AsposeZipLicense = new License(); | |
// Load license from file system into memory stream | |
using (MemoryStream stream = new MemoryStream(File.ReadAllBytes("path to license.lic"))) | |
{ | |
// Set license prior to creating ZIP file in memory | |
AsposeZipLicense.SetLicense(stream); | |
} | |
#region C# code to create ZIP archive in memory | |
// Create MemoryStream instance | |
using (MemoryStream zipFileInMemory = new MemoryStream()) | |
{ | |
// Create empty Archive object for ZIP file | |
using (Aspose.Zip.Archive zipArchive = new Aspose.Zip.Archive()) | |
{ | |
// Get all files from a Folder and store their paths in string Array | |
string[] filesArray = Directory.GetFiles("ZIP all Files\\", "*.*", SearchOption.TopDirectoryOnly); | |
// Loop through the Array and create an ArchiveEntry against each file within the ZIP Archive | |
foreach (string fileDiskPath in filesArray) | |
{ | |
string[] folders = fileDiskPath.Split(new char[] { '\\' }); | |
string fileName = folders[folders.Length - 1]; | |
zipArchive.CreateEntry(fileName, fileDiskPath); | |
} | |
// Call Save method to create ZIP Archive in memory | |
zipArchive.Save(zipFileInMemory); | |
} | |
} | |
#endregion | |
} | |
} | |
} |
Bu nedenle, bellek konsolu uygulamasında bu C# zip dosyası oluşturma, esasen klasörü ZIP biçimine dönüştürmenize ve klasörün tüm içeriğini sıkıştırılmış biçimde belleğe yazmanıza yardımcı olacaktır. Daha sonra MemoryStream C#‘dan ZIP dosyasını yeniden oluşturabilir ve diske kaydedebilirsiniz. Arşiv dosya sisteminde yeniden oluşturulduktan sonra C# kullanarak ZIP dosyasını ayıklayın kodunu kullanabilirsiniz.