This project is read-only.

Compressing custom stream like SharpZipLib.PutNextEntry

Apr 2, 2009 at 8:49 AM
Edited Apr 2, 2009 at 9:12 AM
is not possible to recursively add custom stream like SharpZipLib method PutNexEntry?

it could be useful if you have a list of local files you want to compress, but you want to store it in the archive with a differnt path or name.
Apr 2, 2009 at 9:12 AM
here's an example to explain better:

        FileStream fs = null;
        using (FileStream zipStream = File.Create(zipfile))
            using (ZipOutputStream zip = new ZipOutputStream(zipStream))
                    foreach (string docFileName in docsFilesNames)
                        string customDocFileName = @"CustomDir\CustomeFileName.txt";
                        fs = new FileStream(docFileName, FileMode.Open, FileAccess.Read, FileShare.Read);
                        long nTot = fs.Length;
                        if (nTot > 0L)
                            const int BUF_SIZE = 32768;
                            byte[] buf = new byte[BUF_SIZE];
                            ZipEntry zippedFile = new ZipEntry(customDocFileName);
                            zippedFile.CompressionMethod = CompressionMethod.Deflated;
                            for (long nNow = 0; nNow < nTot; )
                                int nNew = fs.Read(buf, 0, BUF_SIZE);
                                zip.Write(buf, 0, nNew);
                                nNow += nNew;
Apr 2, 2009 at 12:48 PM
How difficult it is with the SharpZipLib! My implementation is going to be much simpler...
Apr 2, 2009 at 1:21 PM

for my project i need in particular the way to tell a different path and name for files that I want to compress in archive.
CompressFiles or CompressStream ways is the same...

Thanks for the quick response!

(ps) level of compression options and archive in volumes would be interesting implementations!! ;-)
Apr 5, 2009 at 2:37 PM
I implemented it in the 0.36 release.
You are to write something like

            SevenZipCompressor tmp = new SevenZipCompressor();
            Dictionary<string, string> fileDict = new Dictionary<string, string>();
            fileDict.Add(@"d:\Temp\08022009.jpg", "photo.jpg");
            tmp.FileCompressionStarted += new EventHandler<FileInfoEventArgs>((o, e) =>
                Console.WriteLine(String.Format("[{0}%] {1}",
                        e.PercentDone, e.FileName));
            tmp.CompressFileDictionary(fileDict, @"d:\Temp\arch.7z", OutArchiveFormat.SevenZip);

You can use Dictionary<Stream, string> for explicit streams archiving - and the corresponding CompressStreamDictionary method.
Apr 6, 2009 at 9:08 AM
perfect! exactly what I needed!
thanks again!
Oct 8, 2009 at 8:56 AM
Edited Oct 8, 2009 at 10:28 AM


I tried this function, but it threw the exception.

SevenZipSharp version 0.58

The execution has failed due to the bug in the SevenZipSharp.
Please report about it to, post the release number and attach the archive.

I modified the code of the 85th line in the class "SevenZipTestForms.FormMain" -> "Compress()"

            if (!sfxMode)
                //original code
                //cmp.CompressDirectory(directory, archFileName);

                System.Collections.Generic.Dictionary<string, string> fileDict =
                    new System.Collections.Generic.Dictionary<string, string>();

                foreach (string file_path in Directory.GetFiles(directory))
                    fileDict.Add(file_path, Path.GetFileName(file_path));

                cmp.CompressFileDictionary(fileDict, archFileName); //throw exception




SevenZipSharp 0.58
Oct 24, 2009 at 1:22 PM
Edited Oct 24, 2009 at 1:27 PM

I have the same problem. Release 0.36 release works, but 0.58 not.

My intention is to add file to the specific path in archive (something like "c:\Temp\archive.7z\TestFolder"). I thought that this could work:

            SevenZipCompressor compressor = new SevenZip.SevenZipCompressor();
            compressor.CustomParameters.Add("s", "off");
            //compressor.CompressionMode = CompressionMode.Create;
            //compressor.CompressionMode = CompressionMode.Append;
            Dictionary<string, string> fileDict = new Dictionary<string, string>();
            fileDict.Add(@"C:\Temp\Testcompressor.CompressFileDictionary(fileDict, @"C:\Temp\archive.7z");

SevenZipCompressor compressor = new SevenZip.SevenZipCompressor();
compressor.CompressionMode = CompressionMode.Append;

Dictionary<string, string> fileDict = new Dictionary<string, string>();
fileDict.Add(@"C:\Temp\Test.pdf", @"TestFolder\Test.pdf");

compressor.CompressFileDictionary(fileDict, @"C:\Temp\archive.7z");


But it doesn't work. Is there way how to accomplish this?

Thanks for any response



Oct 26, 2009 at 10:00 AM

Same problem for me: method CompressFileDictionary works for 0.36 release but 0.58 not.