7
Answers

SEVENZIP FOLDER COMPRESSION. USING SEVENZIP DLLS

Ervin Slavotic

Ervin Slavotic

11y
3.9k
1
Hi All,

I need help in this code... for compressing directories in c# using sevensharp dlls.  http://sevenzipsharp.codeplex.com/  ...
so far the follwing code will create an empyt archvie... so how to add files to it?...



class SevenSync
    {
        public void CompressFolder(string FolderToCompress, string destination)
        {
            List<string> subfiles = new List<string>(Directory.GetFiles(FolderToCompress));
            FileInfo fi = new FileInfo(FolderToCompress);
            StringBuilder output_7zip_File = new StringBuilder(FolderToCompress + Path.DirectorySeparatorChar + fi.Name + @".7z");
            string output_stringBuilder = output_7zip_File.ToString();

            Console.WriteLine("Output destination : " + output_stringBuilder);

            

            foreach (string file in subfiles)
            {
                Console.WriteLine("Files to Compress : " + file);
               // compressor.BeginCompressFiles(output_stringBuilder, file);
                CompressFileLZMA(file, output_stringBuilder);
                AddToArchive(file);
            }


            
        }


        //___________________________________________________________
        ////////////////////////////////////////////////////////////|
        //         C O M P R E S S  F I L E                         |                                           
        //_________using LZMA algo__________________________________|
        ////////////////////////////////////////////////////////////|
        public void CompressFileLZMA(string inFile, string outFile)
        {
            Int32 dictionary = 1 << 23;
            Int32 posStateBits = 2;
            Int32 litContextBits = 3; // for normal files
            // UInt32 litContextBits = 0; // for 32-bit data
            Int32 litPosBits = 0;
            // UInt32 litPosBits = 2; // for 32-bit data
            Int32 algorithm = 2;
            Int32 numFastBytes = 128;

            string mf = "bt4";
            bool eos = true;
            bool stdInMode = false;


            CoderPropID[] propIDs =  {
                CoderPropID.DictionarySize,
                CoderPropID.PosStateBits,
                CoderPropID.LitContextBits,
                CoderPropID.LitPosBits,
                CoderPropID.Algorithm,
                CoderPropID.NumFastBytes,
                CoderPropID.MatchFinder,
                CoderPropID.EndMarker
            };
            
            object[] properties = {
                (Int32)(dictionary),
                (Int32)(posStateBits),
                (Int32)(litContextBits),
                (Int32)(litPosBits),
                (Int32)(algorithm),
                (Int32)(numFastBytes),
                mf,
                eos
            };


            try
            {
                using (FileStream inStream = new FileStream(inFile, FileMode.Open))
                {
                    using (FileStream outStream = new FileStream(outFile, FileMode.Create))
                    {
                        SevenZip.Compression.LZMA.Encoder encoder = new SevenZip.Compression.LZMA.Encoder();
                        encoder.SetCoderProperties(propIDs, properties);
                        encoder.WriteCoderProperties(outStream);
                        Int64 fileSize;
                        if (eos || stdInMode)
                            fileSize = -1;
                        else
                            fileSize = inStream.Length;
                        for (int i = 0; i < 8; i++)
                        {
                            outStream.WriteByte((Byte)(fileSize >> (8 * i)));


                        }
                        encoder.Code(inStream, outStream, -1, -1, null);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("ERROR : " + e.Message);
            }


        }

        
        //___________________________________________________________
        ////////////////////////////////////////////////////////////|
        //       A D D   TO   A R C H I V E                         |                                           
        //__________________________________________________________|
        ////////////////////////////////////////////////////////////|

        public void AddToArchive(string archiveFile)
        {


            

        }


    }




thanks alot 

regards
Answers (7)
0
Javeed M Shaikh

Javeed M Shaikh

NA 7.8k 69.7k 11y
sorry took a while to figure out a few things, try this:

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using SevenZip;
using SevenZip.Sdk.Compression.Lzma;

namespace myspace
{

    class MainClass
    {


        public static void Main()
        {

            SevenSync sy = new SevenSync();
            sy.CompressFolder(@"C:\Temp", @"C:\");

        }
    }



    class SevenSync
    {

        public void CompressFolder(string FolderToCompress, string destination)
        {
            List<string> subfiles = new List<string>(Directory.GetFiles(FolderToCompress));
            FileInfo fi = new FileInfo(FolderToCompress);
            StringBuilder output_7zip_File = new StringBuilder(destination + Path.DirectorySeparatorChar + fi.Name + @".7z");
            string output_stringBuilder = output_7zip_File.ToString();

            Console.WriteLine("Output destination : " + output_stringBuilder);
            SevenZipCompressor cmp = new SevenZipCompressor();
            SevenZipCompressor.SetLibraryPath(@"C:\Program Files\7-Zip\7z.dll");
            cmp.CompressionMode = CompressionMode.Append;

            foreach (string file in subfiles)
            {
                Console.WriteLine("Files to Compress : " + file);
                // compressor.BeginCompressFiles(output_stringBuilder, file);
                cmp.CompressFiles(output_stringBuilder, file);
            }



        }

    }
}
Accepted
0
Ervin Slavotic

Ervin Slavotic

NA 10 5.7k 11y
GOD BLESS U
0
Javeed M Shaikh

Javeed M Shaikh

NA 7.8k 69.7k 11y
I am just setting the path for DLL, you can copy/referece the DLL in you project and that can become a part of your deployment. Please make sure you are using 64/32 version of the DLL before you deploy.
0
Ervin Slavotic

Ervin Slavotic

NA 10 5.7k 11y
I understand where u coming from with this approach this approach... and yea it works!...

However, Its only good if your dealing with data on ur local machine... wheras i was looking for a referenced dll within the solution so the .exe program can be executed from a shatred network.....

any ideas on that?
0
Ervin Slavotic

Ervin Slavotic

NA 10 5.7k 11y
its still shwoing the same thing bro... i mean the archive is created however when i attemt to open the archive it does not open... its hopwing as a corrupted archive.......any other ideas?
0
Ervin Slavotic

Ervin Slavotic

NA 10 5.7k 11y
 thanks bro ill check it out now...                
0
Javeed M Shaikh

Javeed M Shaikh

NA 7.8k 69.7k 11y
I downloaded the DLL and created my own project to test this and looks like your code is ok, I made one change to use the "destination" variable. Please check this:

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using SevenZip;
using SevenZip.Sdk.Compression.Lzma;

namespace myspace
{

    class MainClass
    {


        public static void Main()
        {
            SevenSync sy = new SevenSync();
            sy.CompressFolder(@"C:\Temp",@"C:\");
        }
    }

    class SevenSync
    {

        public void CompressFolder(string FolderToCompress, string destination)
        {
            List<string> subfiles = new List<string>(Directory.GetFiles(FolderToCompress));
            FileInfo fi = new FileInfo(FolderToCompress);
            StringBuilder output_7zip_File = new StringBuilder(destination + Path.DirectorySeparatorChar + fi.Name + @".7z");
            string output_stringBuilder = output_7zip_File.ToString();

            Console.WriteLine("Output destination : " + output_stringBuilder);



            foreach (string file in subfiles)
            {
                Console.WriteLine("Files to Compress : " + file);
                // compressor.BeginCompressFiles(output_stringBuilder, file);
                CompressFileLZMA(file, output_stringBuilder);
                AddToArchive(file);
            }



        }


        //___________________________________________________________
        ////////////////////////////////////////////////////////////|
        //         C O M P R E S S  F I L E                         |                                           
        //_________using LZMA algo__________________________________|
        ////////////////////////////////////////////////////////////|
        public void CompressFileLZMA(string inFile, string outFile)
        {
            Int32 dictionary = 1 << 23;
            Int32 posStateBits = 2;
            Int32 litContextBits = 3; // for normal files
            // UInt32 litContextBits = 0; // for 32-bit data
            Int32 litPosBits = 0;
            // UInt32 litPosBits = 2; // for 32-bit data
            Int32 algorithm = 2;
            Int32 numFastBytes = 128;

            string mf = "bt4";
            bool eos = true;
            bool stdInMode = false;


            SevenZip.Sdk.CoderPropId[] propIDs =  {
                SevenZip.Sdk.CoderPropId.DictionarySize,
                SevenZip.Sdk.CoderPropId.PosStateBits,
                SevenZip.Sdk.CoderPropId.LitContextBits,
                SevenZip.Sdk.CoderPropId.LitPosBits,
                SevenZip.Sdk.CoderPropId.Algorithm,
                SevenZip.Sdk.CoderPropId.NumFastBytes,
                SevenZip.Sdk.CoderPropId.MatchFinder,
                SevenZip.Sdk.CoderPropId.EndMarker
            };

            object[] properties = {
                (Int32)(dictionary),
                (Int32)(posStateBits),
                (Int32)(litContextBits),
                (Int32)(litPosBits),
                (Int32)(algorithm),
                (Int32)(numFastBytes),
                mf,
                eos
            };


            try
            {
                using (FileStream inStream = new FileStream(inFile, FileMode.Open))
                {
                    using (FileStream outStream = new FileStream(outFile, FileMode.Create))
                    {
                        SevenZip.Sdk.Compression.Lzma.Encoder encoder = new SevenZip.Sdk.Compression.Lzma.Encoder();
                        encoder.SetCoderProperties(propIDs, properties);
                        encoder.WriteCoderProperties(outStream);
                        Int64 fileSize;
                        if (eos || stdInMode)
                            fileSize = -1;
                        else
                            fileSize = inStream.Length;
                        for (int i = 0; i < 8; i++)
                        {
                            outStream.WriteByte((Byte)(fileSize >> (8 * i)));


                        }
                        encoder.Code(inStream, outStream, -1, -1, null);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("ERROR : " + e.Message);
            }


        }


        //___________________________________________________________
        ////////////////////////////////////////////////////////////|
        //       A D D   TO   A R C H I V E                         |                                           
        //__________________________________________________________|
        ////////////////////////////////////////////////////////////|

        public void AddToArchive(string archiveFile)
        {




        }
    }
}