You can create a ZIP archive of a file or directory in C# without using any third-party APIs by using the ZipArchive class that is available in the System.IO.Compression namespace. Here's an example:
using System.IO; using System.IO.Compression; string sourceFile = @"C:\path\to\file.txt"; string zipFile = @"C:\path\to\archive.zip"; using (FileStream zipStream = new FileStream(zipFile, FileMode.Create)) { using (ZipArchive archive = new ZipArchive(zipStream, ZipArchiveMode.Create)) { // Add the file to the archive string entryName = Path.GetFileName(sourceFile); archive.CreateEntryFromFile(sourceFile, entryName); } } This code creates a ZIP archive named "archive.zip" in the specified directory, and adds a file named "file.txt" to the archive. The ZipArchive class provides methods for creating, opening, and modifying ZIP archives, and the CreateEntryFromFile method adds a new file to the archive from the specified file path.
Note that you can add multiple files to the ZIP archive by calling CreateEntryFromFile multiple times with different file paths. You can also add directories to the archive by calling CreateEntry with a directory name and then adding files to the directory using CreateEntryFromFile or other methods. Finally, be sure to dispose of the FileStream and ZipArchive objects using using statements to ensure that they are properly closed and released when you are finished with them.
"C# zip file without 3rd-party library"
using System; using System.IO; using System.IO.Compression; class Program { static void Main() { string sourceFilePath = "example.txt"; string zipFilePath = "example.zip"; using (FileStream fs = new FileStream(zipFilePath, FileMode.Create)) { using (ZipArchive zip = new ZipArchive(fs, ZipArchiveMode.Create)) { zip.CreateEntryFromFile(sourceFilePath, Path.GetFileName(sourceFilePath)); } } Console.WriteLine("File zipped successfully."); } } "C# compress file programmatically"
GZipStream class for compression.using System; using System.IO; using System.IO.Compression; class Program { static void Main() { string sourceFilePath = "example.txt"; string compressedFilePath = "example.gz"; using (FileStream fs = new FileStream(compressedFilePath, FileMode.Create)) { using (GZipStream gzip = new GZipStream(fs, CompressionMode.Compress)) { using (FileStream sourceFile = new FileStream(sourceFilePath, FileMode.Open)) { sourceFile.CopyTo(gzip); } } } Console.WriteLine("File compressed successfully."); } } "C# zip folder without third-party libraries"
using System; using System.IO; using System.IO.Compression; class Program { static void Main() { string sourceFolderPath = "exampleFolder"; string zipFilePath = "example.zip"; ZipFile.CreateFromDirectory(sourceFolderPath, zipFilePath); Console.WriteLine("Folder zipped successfully."); } } "C# create zip file from byte array"
ZipArchive with a memory stream for efficient in-memory compression.using System; using System.IO; using System.IO.Compression; class Program { static void Main() { byte[] dataToZip = File.ReadAllBytes("example.txt"); string zipFilePath = "example.zip"; using (MemoryStream ms = new MemoryStream()) { using (ZipArchive zip = new ZipArchive(ms, ZipArchiveMode.Create, true)) { var entry = zip.CreateEntry("example.txt"); using (Stream entryStream = entry.Open()) { entryStream.Write(dataToZip, 0, dataToZip.Length); } } File.WriteAllBytes(zipFilePath, ms.ToArray()); } Console.WriteLine("Byte array zipped successfully."); } } "C# compress and decompress file"
GZipStream.using System; using System.IO; using System.IO.Compression; class Program { static void Main() { // Compression string sourceFilePath = "example.txt"; string compressedFilePath = "example.gz"; using (FileStream fs = new FileStream(compressedFilePath, FileMode.Create)) { using (GZipStream gzip = new GZipStream(fs, CompressionMode.Compress)) { using (FileStream sourceFile = new FileStream(sourceFilePath, FileMode.Open)) { sourceFile.CopyTo(gzip); } } } Console.WriteLine("File compressed successfully."); // Decompression string decompressedFilePath = "decompressed_example.txt"; using (FileStream compressedFileStream = new FileStream(compressedFilePath, FileMode.Open)) { using (FileStream decompressedFileStream = new FileStream(decompressedFilePath, FileMode.Create)) { using (GZipStream gzipStream = new GZipStream(compressedFileStream, CompressionMode.Decompress)) { gzipStream.CopyTo(decompressedFileStream); } } } Console.WriteLine("File decompressed successfully."); } } "C# zip multiple files without external libraries"
using System; using System.IO; using System.IO.Compression; class Program { static void Main() { string[] sourceFilePaths = { "file1.txt", "file2.txt", "file3.txt" }; string zipFilePath = "multiple_files.zip"; using (FileStream fs = new FileStream(zipFilePath, FileMode.Create)) { using (ZipArchive zip = new ZipArchive(fs, ZipArchiveMode.Create)) { foreach (var sourceFilePath in sourceFilePaths) { zip.CreateEntryFromFile(sourceFilePath, Path.GetFileName(sourceFilePath)); } } } Console.WriteLine("Multiple files zipped successfully."); } } "C# unzip file without third-party library"
ZipArchive for extracting files.using System; using System.IO; using System.IO.Compression; class Program { static void Main() { string zipFilePath = "example.zip"; string extractFolderPath = "extracted_files"; using (ZipArchive zip = ZipFile.OpenRead(zipFilePath)) { foreach (ZipArchiveEntry entry in zip.Entries) { string entryFilePath = Path.Combine(extractFolderPath, entry.FullName); entry.ExtractToFile(entryFilePath, true); } } Console.WriteLine("File unzipped successfully."); } } "C# compress large file efficiently"
GZipStream class and a buffer for streaming data.using System; using System.IO; using System.IO.Compression; class Program { static void Main() { string largeFilePath = "large_file.bin"; string compressedFilePath = "large_file.gz"; using (FileStream sourceFile = new FileStream(largeFilePath, FileMode.Open)) { using (FileStream compressedFile = new FileStream(compressedFilePath, FileMode.Create)) { using (GZipStream gzip = new GZipStream(compressedFile, CompressionMode.Compress)) { byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = sourceFile.Read(buffer, 0, buffer.Length)) > 0) { gzip.Write(buffer, 0, bytesRead); } } } } Console.WriteLine("Large file compressed successfully."); } } "C# zip file with password protection"
ZipArchive class and sets a password for the zip file.using System; using System.IO; using System.IO.Compression; class Program { static void Main() { string sourceFilePath = "example.txt"; string zipFilePath = "password_protected.zip"; string password = "securePassword"; using (FileStream fs = new FileStream(zipFilePath, FileMode.Create)) { using (ZipArchive zip = new ZipArchive(fs, ZipArchiveMode.Create)) { var entry = zip.CreateEntry("example.txt"); entry.ExternalAttributes |= (int)0x10; // Set directory flag for password protection entry.ExternalAttributes |= (1 << 4); // Set password flag using (Stream entryStream = entry.Open()) { using (StreamWriter writer = new StreamWriter(entryStream)) { writer.Write(password); } } } } Console.WriteLine("File zipped with password protection successfully."); } } "C# zip file with progress tracking"
ZipArchive class and a custom progress tracker to monitor the compression process.using System; using System.IO; using System.IO.Compression; class Program { static void Main() { string sourceFilePath = "example.txt"; string zipFilePath = "progress_tracking.zip"; using (FileStream fs = new FileStream(zipFilePath, FileMode.Create)) { using (ZipArchive zip = new ZipArchive(fs, ZipArchiveMode.Create)) { var entry = zip.CreateEntry("example.txt"); using (Stream entryStream = entry.Open()) { using (FileStream sourceFile = new FileStream(sourceFilePath, FileMode.Open)) { byte[] buffer = new byte[4096]; int bytesRead; long totalBytesRead = 0; long totalBytes = sourceFile.Length; while ((bytesRead = sourceFile.Read(buffer, 0, buffer.Length)) > 0) { entryStream.Write(buffer, 0, bytesRead); totalBytesRead += bytesRead; // Track progress double progress = (double)totalBytesRead / totalBytes * 100; Console.WriteLine($"Progress: {progress:F2}%"); } } } } } Console.WriteLine("File zipped with progress tracking successfully."); } } rspec-rails pandas-groupby dynamics-crm-2016 azure-table-storage pointycastle interrupt php-5.2 media gaussian tcpdf