Using the DotNetZip Library (http://dotnetzip.codeplex.com/) is there a way to move files from one zip file into another without extracting that file to disk first? Maybe extract to a stream, then update into the other zip from that same stream?
The zip files are password protected and the data in these zip files are meant to stay that way due to their licenses. If I simply extract to disk first then update the other zip there is a chance where those files can be intercepted by the user.
Yes, you should be able to do something like;
var ms = new MemoryStream();
using (ZipFile zip = ZipFile.Read(sourceZipFile))
{
zip.Extract("NameOfEntryInArchive.doc", ms);
}
ms.Seek(0);
using (ZipFile zip = new ZipFile())
{
zip.AddEntry("NameOfEntryInArchive.doc", ms);
zip.Save(zipToCreate);
}
(see it as pseudocode since I didn't have a chance to compile)
Naturally you'll have to add your decryption/encryption to that, but those calls are equally straight forward.
Related
I have a large zip file (let's say 10 GB), to which I want to add a single small file (let's say 50 KB). I'm using the following code:
using System.IO.Compression;
using (var targetZip = ZipFile.Open(largeZipFilePath), ZipArchiveMode.Update)
{
targetZip.CreateEntryFromFile(smallFilePath, "foobar");
}
While this works (eventually), it takes a very long time and consumes a ludicrous amount of memory. It seems to extract and recompress the whole archive.
How can I improve this in .Net 4.7? Solution without external dependencies is preferred, but not required if impossible.
use visual studio nuget package manager and install that
Install-Package DotNetZip -Version 1.11.0
using (ZipFile zip = new ZipFile())
{
zip.AddFile("ReadMe.txt"); // no password for this one
zip.Password= "123456!";
zip.AddFile("7440-N49th.png");
zip.Password= "!Secret1";
zip.AddFile("2005_Annual_Report.pdf");
zip.Save("Backup.zip");
}
https://www.nuget.org/packages/DotNetZip/
Since you are in above .NET 4.5, you can use the ZipArchive (System.IO.Compression) class to achieve this. Here is the MSDN documentation: (MSDN).
Here is their example, it just writes text, but you could read in a .csv file and write it out to your new file. To just copy the file in, you would use CreateFileFromEntry, which is an extension method for ZipArchive.
using (FileStream zipToOpen = new FileStream(#"c:\users\exampleuser\release.zip", FileMode.Open))
{
using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Update))
{
ZipArchiveEntry readmeEntry = archive.CreateEntry("Readme.txt");
using (StreamWriter writer = new StreamWriter(readmeEntry.Open()))
{
writer.WriteLine("Information about this package.");
writer.WriteLine("========================");
}
}
}
Check this:- https://stackoverflow.com/a/22339337/9912441
https://learn.microsoft.com/en-us/dotnet/standard/io/how-to-compress-and-extract-files
I found the reason for this behaviour in another Stack Overflow answer: Out of memory exception while updating zip in c#.net.
The gist of it is that this takes a long time because ZipArchiveMode.Update caches the zip file into memory. The suggestion for avoiding this caching behaviour is to create a new archive, and copy the old archive contents along with the new file to it.
See the MSDN documentation which explains how ZipArchiveMode.Update behaves:
I'm trying to export the pdf files from Crystal to streams, then I want to add them (7 total) to a zip file using the DotNetZip Library. I'm just trying to add one below. I have a feeling I'm way off. Please help.
MemoryStream oStream;
CrystalDecisions.CrystalReports.Engine.ReportDocument rpt = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
if (a2batch.Count > 0) // a2batch - My Crystal Datasource List
{
rpt.Load(Server.MapPath("~\\Report\\Construction\\ScheduleA2.rpt"));
rpt.SetDataSource(a2batch);
oStream = (MemoryStream)rpt.ExportToStream(ExportFormatType.PortableDocFormat);
using (ZipFile zipFile = new ZipFile())
{
zipFile.AddEntry("Report.pdf", oStream);
zipFile.Save("Report.zip");
}
}
Does the code in your question work? If so, the easiest is probably to just open the zip file when you add the other streams:
using (ZipFile zip = ZipFile.Read(ExistingZipFile))
Then use the same code as in you question to add the new file, and then save it with:
zip.Save();
You can add and remove files to an existing zip archive at will. If you are creating all the streams in the same method, it is also possible to just add data from several streams before you close the file, that is adding several zip.AddEntry statements after each other.
this is my first question on here, so bear with me.
What I'm aiming to do is just create a basic .zip archive in C#. I have tried using the built-in GZipStream class of .NET and have managed to accomplish this, but then I have the problem that I cannot name the file "usercode.zip" without the archived file losing it's extension. Due to constraints I cannot make my program create these files as "usercode.trf.zip", which is the only way I've found of leaving the filename's extension intact inside the archive.
I've tried using a number of other zipping libraries and I can't seem to manage getting them working properly or the way I want it to.
I came upon the SevenZipHelper library that provides some nifty functions to use the LZMA (or 7-zip) library to compress a file.
The code I'm using looks as follows:
//Take the BF file and zip it, using 7ZipHelper
BinaryReader bReader = new BinaryReader(File.Open(pFileName, FileMode.Open));
byte[] InBuf = new byte[Count];
bReader.Read(InBuf, 0, InBuf.Length);
Console.WriteLine("ZIP: read for buffer length:" + InBuf.Length.ToString());
byte[] OutBuf = SevenZip.Compression.LZMA.SevenZipHelper.Compress(InBuf);
FileStream BZipFile = new FileStream(pZipFileName, FileMode.OpenOrCreate, FileAccess.Write);
BZipFile.Seek(0, SeekOrigin.Begin);
BZipFile.Write(OutBuf, 0, OutBuf.Length);
BZipFile.Close();
This creates a compressed file neatly, using the 7-zip algorithm. Problem is I can't guarantee that the clients using this program will have access to 7-zip, so the file has to be in normal zip algorithm. I've gone through the helper- as well as the 7-zip libraries and it seems it is possible to use this library to compress a file with the normal "ZIP" algorithm. I just cannot seem to figure out how to do this. I've noticed properties settings in a few places, but I cannot find any documentation or googling to tell me where to set this.
I realize there's probably better ways to do this and that I'm just missing something, but I can't sit and struggle with such a supposedly easy task forever. Any help would be greatly appreciated.
If you want you can take a look at this library, I've used it before and it's preaty simple to use : dotnetzip
EDIT(example):
using (ZipFile zip = new ZipFile())
{
foreach (String filename in FilesList)
{
Console.WriteLine("Adding {0}...", filename);
ZipEntry e = zip.AddFile(filename,"");
e.Comment = "file " +filename+" added "+DateTime.Now;
}
Console.WriteLine("Done adding files to zip:" + zipName);
zip.Comment = String.Format("This zip archive was created by '{0}' on '{1}'",
System.Net.Dns.GetHostName(), DateTime.Now);
zip.Save(zipName);
Console.WriteLine("Zip made:" + zipName);
}
I have a .tar file containing multiple compressed .gz files. I have no issue itterating through the .tar file creating each .gz file in a destination directory. I'd like to skip writting the .gz all together and just decompress it from the TarEntry/TarArchive? and write its contents on the fly via the .Net native GZipStream. Not even sure this is possible.
Here is my current code that writes each g'zipped file out. Not sure what to modify to get where I need to be.
using (FileStream _fsIn = new FileStream(#"F:\data\abc.tar", FileMode.Open, FileAccess.Read))
{
using (TarInputStream _tarIn = new TarInputStream(_fsIn))
{
TarEntry _tarEntry;
while ((_tarEntry = _tarIn.GetNextEntry()) != null)
{
string _archiveName = _tarEntry.Name;
using (FileStream _outStr = new FileStream(#"F:\data\" + _archiveName, FileMode.Create))
{
_tarIn.CopyEntryContents(_outStr);
}
}
}
}
I'am not sure what you want to do. Maybe you can clarify your aim. The sharpzlib is not that good documented as I Expected to be.
I've iterated through a tar archive and pushed the content of a file into a new Stream, maybe you can use this as a starting point. Have a look at this StackOverflow Article
How can I zip (In the server) multiple files to one archive?
Following code uses our Rebex ZIP and shows how to add files into the ZIP archive without using any temp file. The ZIP is then sent to the web browser.
// prepare MemoryStream to create ZIP archive within
using (MemoryStream ms = new MemoryStream())
{
// create new ZIP archive within prepared MemoryStream
using (ZipArchive zip = new ZipArchive(ms))
{
// add some files to ZIP archive
zip.Add(#"c:\temp\testfile.txt");
zip.Add(#"c:\temp\innerfile.txt", #"\subfolder");
// clear response stream and set the response header and content type
Response.Clear();
Response.ContentType = "application/zip";
Response.AddHeader("content-disposition", "filename=sample.zip");
// write content of the MemoryStream (created ZIP archive)
// to the response stream
ms.WriteTo(Response.OutputStream);
}
}
// close the current HTTP response and stop executing this page
HttpContext.Current.ApplicationInstance.CompleteRequest();
For more info see ZIP tutorial.
Alternative solution:
SharpZipLib and DotNetZip a widely used free alternatives.
Take a look at SharpZipLib or DotNetZip
Codeplex has Dot Net Zip http://dotnetzip.codeplex.com/
You can also try System.IO.Compression