I need to compress a file as 7zip using SharpCompress: http://sharpcompress.codeplex.com
what I have done as follows:
using (var archive = ZipArchive.Create())
{
archive.AddEntry("CompressionTest.pdb", new FileInfo("CompressionTest.pdb"));
using (Stream newStream = File.Create("CompressionTest212.7z"))
{
archive.SaveTo(newStream, SharpCompress.Common.CompressionType.LZMA);
}
}
The compression process is done successfully. However, the compressed file can not be extracted either using 7z (http://www.7-zip.org/download.html) or winrar.
I dont know if somebody also got the same problem and had an idea how to solve it?
Thanks in advance.
I'm the author of SharpCompress (thanks for trying it out by the way) and 7Zip compression isn't supported: http://sharpcompress.codeplex.com/wikipage?title=Supported%20formats
What you wrote is code for creating a standard Zip file with LZMA compression. It's possible my code does not create a proper zip file but it's also possible that the created file can't be read by all programs. The Zip format allows for LZMA compression but not all programs may expect it. PeaZip (based on the 7Zip archiver code) does extract a Zip with LZMA, but WinRAR does not.
If you really need the 7Zip format, I do suggest using something else. Personally, I think the 7Zip format is overly complex and recommend Zip or Tar then just pick your compression of choice.
SharpCompress doesn't support 7zip compression. Only decompression, see: http://sharpcompress.codeplex.com/ ( Supported Format Table )
You can use the native library of 7zip for compression, or use an opensource wrapper around it like: http://sevenzipsharp.codeplex.com/
Related
I have a question according to the ZipArchive Library in System.IO.Compression.
I want to create an uncompressed .zip file. My code so far looks like this:
//Creates a "Deflate"-Mode file in the created zip.
using (FileStream fs = new FileStream(zippath, FileMode.OpenOrCreate))
using (ZipArchive zip = new ZipArchive(fs, ZipArchiveMode.Update))
{
var demoFile = zip.CreateEntry("foo0.txt", CompressionLevel.NoCompression); //NoCompression does not seem to have an impact
using (var streamWriter = new StreamWriter(demoFile.Open()))
{
streamWriter.Write("Bar!");
}
}
Thats creating me a zip file, where the file in it was written in "DEFLATE" Mode not in STORE. How can I fix this. My thought was, my problem would be solved by using the CompressionLevel.NoCompression.
Also writing the file to the filesystem and zipping the directory is not an option, because i want to create a zipfile with potentially hundred of thousands small files. Furthermore just using GZipStream is not an option, because I want to create a directory structure in the .zip file.
I checked the mode with 7-zip:
(screenshot from 7-zip)
If for whatever reason you are required to add contents to a ZIP file with its compression method explicitly set to STORE (no compression), you will need to use some third party library.
The .NET classes in System.IO.Compression use DEFLATE by default. There is no apparent way to change this and use another compression method or algorithm.
Providing CompressionLevel.NoCompression just tells the DEFLATE algorithm to work with the lowest compression rate1. In terms of file size, this will probably give you roughly the same end result, anyway.
Third party libraries supporting the method STORE include:
SharpCompress
(see supported formats)
SharpZipLib
(see compression methods)
DotNetZip
1 which should be... no compression. See DEFLATE's non-compressed blocks
For anyone who happens to see this topic later on, I would highly recommend the ZipStorer class by Jaime Olivares:
https://github.com/jaime-olivares/zipstorer
It's easy to add this code to a C# project (not a DLL), and it's easy to add files using 'store' instead of 'deflate'.
I have a single file, Setup1.cab, which is split up into Setup1.zip.001 and Setup1.zip.002 that I used 7zip to archive. Once those volumes reach their destination, I'd like to be able to use C# to extract that file from both archives into the same directory where they will reside. Is this something that SharpZipLib is capable of, or should I be using another tool?
Otherwise, is there a way to combine the two using C# (or another tool - I'm open!) into one zip file, THEN extract it using SharpZipLib?
Thanks!
EDIT: 7zip will not be installed on the destination machines. Also, I'm open to using a different method of archiving the original file; I just need it to be in chunks of under 500MB, and the original file is 570MB.
I would take a look at the SevenZipSharp library and actually use 7zip via C# to handle the decompression.
All,
I have a requirement to Compress an XML file. At the moment I am using C# and the gzip algorithm in the .NET Classes. I does compress it but not at the rate I would like to.
For example a 12MB file was compressed to a little less than 4MB.
Is there any other way to compress it more than that? Speed of compression / decompression is not very important.
Thanks,
M
ZIP compression is well suited for compressing XML data. In .NET you best rely on third party libraries:
DotNetZip
SharpZipLib
You may try 7zip.
7-zip has an SDK.
Use the client version of 7-zip to try different compression settings to find the one with best compression for your particular data set.
This website compared different compression libraries against large amount of text data. 7-zip is also included. I hope that this helps you to choose correct library that matches your requirements.
Take a look at System.IO.Packaging.ZipPackage in WindowsBase. It's the .NET framework code behind the DOCX & XLSX file formats and these are more or less zipped XML files. You can zip multiple files of any format together, not just XML.
I have hundreds of CSV files zipped. This is great because they take very little space but when it is time to use them, I have to make some space on my HD and unzip them before I can process. I was wondering if it is possible with .NET to unzip a file while reading it. In other words, I would like to open a zip file, start to decompress the file and as we go, process the file.
So there would be no need for extra space on my drive. Any ideas or suggestions?
Yes. Zip is a streamed format which means that you can use the data as you decompress it rather than having to decompress everything first.
With .net's System.IO.Compression classes you can apply similar compression as used in zip files (Deflate & GZip) to any stream you like, but if you want to work with actual zip format files you'll need a third party library like this one (sharpziplib).
A better solution might be to keep the files decompressed on the drive, but turn on compression on the file system level. This way you'll just be reading CSV files, and the OS will take care of making sure it doesn't take too much space.
Anyhoo, to answer your question, maybe the GZipStream class can help you.
sharpziplib allows for stream-based decompression - see this related question - the item provides similar stream-based Read methods, so you can process each item like you would with any stream.
I'm not sure about zip files, but you could use GZ format with GZipSteam (works like any other input stream). Unfortunately, the entire System.IO.Compression namespace is only 2 classes (the other does DEFLATE).
EDIT: There's a class called ZipPackage. I'm not sure how if it will let you do decompression streaming, but it might be worth looking into.
Also, take a look at #ziplib.
How can i download a zipped folder from a remote server and unzip the entire files in the folder and store them in isolated storage space from a silver light 3 or 4 out of browser application. Any suggestion please
You can download a zip file like any files with the Webclient class, look at the details and examples in the msdn documentation for downloading content on demand it even talks about how to download and get a specific file from a zip archive.
However if you want to list the files, check out this blogpost, I've not actually tried it but it shows how to get all the files in a zip archive.
Edit: I also found this discussion which offers some ideas, it among other things mentions this Small unzip utility for Silverlight, which seems a bit more robust.
Then use the IsolatedStorageFile class to save the files.
Good Luck!
Ola
For the (un)zipping I'd highly recommend that you use the open source DotNetZip library. DotNetZip is licensed under the Ms-PL and really easy to use.
Zipping for example is easy too:
using (ZipFile zip = new ZipFile())
{
zip.AddEntry("MyFileName.png", null, pngStream);
// Save to stream from SaveFileDialog
zip.Save(stream);
}
Silverlight SharpZipLib is a full Silverlight 3/4 and Phone7 port, less AES encryption, of SharpZipLib.
The salient limitation is one that you will find in all Silverlight compression: Only UTF8 encoding of entry meta is supported.
you could get the file using http or frp stream and then use GZipStream (.NET Class) for unziping the stream/file.
GzipStream:
http://msdn.microsoft.com/en-us/library/system.io.compression.gzipstream.aspx
Cheers
--Jocke