Click here to Skip to main content
15,904,416 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I am writing an application to compress directories. Can somebody please give me a working sample of compressing directories using 7z. SevenZIp only. plzzzz

so far i have this :

List string subfiles = new List string(Directory.GetFiles(inFolder));
foreach (string file in subfiles)
Console.WriteLine(Files to Compress : + file);
add Files to list

AddToArchive(inFolder, splitIntoDir);

public void AddToArchive(string fileToBeZipped, string zipDestination)
DirectoryInfo Di = new DirectoryInfo(zipDestination);
StringBuilder sb_archiveFile = new StringBuilder(zipDestination + Path.DirectorySeparatorChar + Di.Name + @".7z");
string archiveFile = sb_archiveFile.ToString();

SevenZip.SevenZipCompressor compressor = new SevenZipCompressor();

Console.WriteLine("zip destination : " + Di.Name);
if (!File.Exists(fileToBeZipped))
Console.WriteLine("Appending {0} to Archive ", fileToBeZipped);
compressor.CompressionMode = SevenZip.CompressionMode.Append;
Console.WriteLine("Creating {0} at Destination {1}....", fileToBeZipped, archiveFile);//MAKE OUT FILE A CREATED FILE NEWLY ONE
Console.WriteLine("CREATING:: ");
compressor.CompressionMode = SevenZip.CompressionMode.Create;
compressor.CompressionLevel = CompressionLevel.Normal;
compressor.CompressionMethod = CompressionMethod.Lzma;
compressor.CompressionMode = CompressionMode.Append;
compressor.CompressDirectory(zipDestination, archiveFile);

// compressor.CompressStream(streamer, streamer2);

Updated 29-Apr-13 4:58am
ZurdoDev 29-Apr-13 10:58am    
What is wrong with what you have? Where are you stuck?
ervin_Dev 29-Apr-13 11:10am    
Its only archiving individual files with the source folder whereas I want it to add files to an already crerated zip archive..

thanks for reply

1 solution

This is my AddToArchive Method. All this does is creates an archive in destination folder and the archive doesnt even open. Any idea?

public void AddToArchive(string inFile, string outFile)
            FileInfo f = new FileInfo(inFile);

            StringBuilder output_7zip_File = new StringBuilder(outFile + Path.DirectorySeparatorChar + f.Name + @".7z");
            string output_stringBuilder = output_7zip_File.ToString();
             StringBuilder output_File = new StringBuilder(outFile + Path.DirectorySeparatorChar + f.Name);
            string output_dir_stringBuilder = output_7zip_File.ToString();

            SevenZipCompressor szc = new SevenZipCompressor();

            if (File.Exists(inFile))
                szc.CompressionMode = SevenZip.CompressionMode.Append;
                szc.CompressionMode = SevenZip.CompressionMode.Create;
            FileStream archive = new FileStream(output_stringBuilder, FileMode.OpenOrCreate, FileAccess.ReadWrite);

                <pre lang="c#">szc.CompressionLevel = CompressionLevel.Normal;
                szc.CompressionMethod = CompressionMethod.Lzma;
                szc.CompressionMode = CompressionMode.Append;
                szc.DirectoryStructure = false;
                szc.EncryptHeaders = true;
                szc.DefaultItemName = inFile;

szc.CompressDirectory(outFile, archive);

catch (Exception e)
Console.WriteLine("ERROR :::: "+ e.Message);
Console.WriteLine("ERROR :::: " + e.StackTrace);


Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900