Using System.IO.Packaging to generate a ZIP file
let me google this for you -> system.io.packaging+generate+zip
first link
http://weblogs.asp.net/jongalloway//creating-zip-archives-in-net-without-an-external-library-like-sharpziplib
using System;
using System.IO;
using System.IO.Packaging;
namespace ZipSample
{
class Program
{
static void Main(string[] args)
{
AddFileToZip("Output.zip", @"C:\Windows\Notepad.exe");
AddFileToZip("Output.zip", @"C:\Windows\System32\Calc.exe");
}
private static void AddFileToZip(string zipFilename, string fileToAdd, CompressionOption compression = CompressionOption.Normal)
{
using (Package zip = System.IO.Packaging.Package.Open(zipFilename, FileMode.OpenOrCreate))
{
string destFilename = ".\\" + Path.GetFileName(fileToAdd);
Uri uri = PackUriHelper.CreatePartUri(new Uri(destFilename, UriKind.Relative));
if (zip.PartExists(uri))
{
zip.DeletePart(uri);
}
PackagePart part = zip.CreatePart(uri, "", compression);
using (FileStream fileStream = new FileStream(fileToAdd, FileMode.Open, FileAccess.Read))
{
using (Stream dest = part.GetStream())
{
fileStream.CopyTo(dest);
}
}
}
}
}
}
Creating a ZIP file in memory with System.IO.Packaging.Package
The issue seems to be that you will need a relative URI, but the Uri class doesn't know what kind of Uri you're creating.
Try this instead:
new Uri("/" + i + ".gif", UriKind.Relative)
Unpacking zip file in c# using System.IO.Packaging
Apparently, though all System.IO.Packaging files are zip files, not all zip files conform to System.IO.Packaging. As such, it is useless for unpacking zip files.
OutOfMemoryException when creating large ZIP file using System.IO.Packaging
Calling the Flush
method on the Package object in between creating a new package should probably solve the problem as that would cause the memory buffer to be flushed to disk.
Extracting files from a Zip archive programmatically using C# and System.IO.Packaging
If you are manipulating ZIP files, you may want to look into a 3rd-party library to help you.
For example, DotNetZip, which has been recently updated. The current version is now v1.8. Here's an example to create a zip:
using (ZipFile zip = new ZipFile())
{
zip.AddFile("c:\\photos\\personal\\7440-N49th.png");
zip.AddFile("c:\\Desktop\\2005_Annual_Report.pdf");
zip.AddFile("ReadMe.txt");
zip.Save("Archive.zip");
}
Here's an example to update an existing zip; you don't need to extract the files to do it:
using (ZipFile zip = ZipFile.Read("ExistingArchive.zip"))
{
// 1. remove an entry, given the name
zip.RemoveEntry("README.txt");
// 2. Update an existing entry, with content from the filesystem
zip.UpdateItem("Portfolio.doc");
// 3. modify the filename of an existing entry
// (rename it and move it to a sub directory)
ZipEntry e = zip["Table1.jpg"];
e.FileName = "images/Figure1.jpg";
// 4. insert or modify the comment on the zip archive
zip.Comment = "This zip archive was updated " + System.DateTime.ToString("G");
// 5. finally, save the modified archive
zip.Save();
}
here's an example that extracts entries:
using (ZipFile zip = ZipFile.Read("ExistingZipFile.zip"))
{
foreach (ZipEntry e in zip)
{
e.Extract(TargetDirectory, true); // true => overwrite existing files
}
}
DotNetZip supports multi-byte chars in filenames, Zip encryption, AES encryption, streams, Unicode, self-extracting archives.
Also does ZIP64, for file lengths greater than 0xFFFFFFFF, or for archives with more than 65535 entries.
free. open source
get it at
codeplex or direct download from windows.net - CodePlex has been discontinued and archived
System.IO.Packaging library does not read standard windows zip files
System.IO.Packaging
isn't there to read zip files but packages. Use DotnetZip instead.
Is there a way to determine whether a file can be opened as a System.IO.Packaging.Package?
You can either attempt to open the file as a .zip file and then look for metadata (thus implementing part of the package specification where you verify the package format) or you can just use Open()
and catch any potential exception. (The .docx / .xlsx / etc. formats are just ZIP files that follow a certain structure.)
You can try reading the ZIP header but that will only verify that the file is a ZIP file - I'm not sure this buys you much (if anything).
My guess is that trying to open the file and catching the exception is the easiest route to go - if the specification changes your code will keep working. If you roll your own code to verify the file format, you'll have to keep maintaining it.
Related Topics
Sorting a Dictionary in Place with Respect to Keys
C# - How to Copy a Single Excel Worksheet from One Workbook to Another
Get Access to Parent Control from User Control - C#
How to Draw a Rounded Rectangle in C#
How to Draw a Rounded Rectangle with Winforms (.Net)
Applying % Number Format to a Cell Value Using Openxml
Extracting Text from PDFs in C#
Imap Auth in Office 365 Using Oauth2
Creating a Database Programmatically in SQL Server
How to Implement the Sieve of Eratosthenes Using Multithreaded C#
Take Screenshot of Multiple Desktops of All Visible Applications and Forms
In C# Why Can't a Conditional Operator Implicitly Cast to a Nullable Type
String Compare C# - Whole Word Match
How to Prevent Datagridview from Flickering When Scrolling Horizontally