1

I 'm using sharpzip lib for compressing a folder. But After compression zip size is a bit higher than the folder size. My folder includes 3 xml files encrypted with aes/Rsa.

for eg: size = 1kb size on disk = 12 kb

after compression size = 1.32 kb and size on disk 4 kb

ArrayList array = new ArrayList();
int TrimLength = (Directory.GetParent(PathToFile)).ToString().Length;
string test=@"\";

if (Directory.Exists(PathToFile))
{          
    if (!PathToFile.EndsWith(test))
    {
        TrimLength = TrimLength - 1;
    }
    array = GenerateFileList(PathToFile); // generate file list 
}

else
{
    array.Add(PathToFile);
}

FileStream ostream=null;
byte[] obuffer;
string outPath = @"" + CompressToFolder;

ZipOutputStream oZipStream = new ZipOutputStream(File.Create(outPath)); // create zip stream
oZipStream.SetLevel(9); // maximum compression
ZipEntry oZipEntry;

foreach (string Fil in array) // for each file, generate a zipentry
{
    oZipEntry = new ZipEntry(Fil.Remove(0, TrimLength+1));
    oZipEntry.IsUnicodeText = true;
    oZipStream.PutNextEntry(oZipEntry);

    if (!Fil.EndsWith(@"/")) // if a file ends with '/' its a directory
    {
        ostream = File.OpenRead(Fil);
        obuffer = new byte[ostream.Length];

        ostream.Read(obuffer, 0, obuffer.Length);

        oZipStream.Write(obuffer, 0, obuffer.Length);
        ostream.Dispose();
    }
}

ostream.Close();
oZipStream.Finish();
oZipStream.Close();

return true;
JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
  • I don't think encrypted stuff compresses as well as plain text. That probably has something to do with it. – RobSiklos Dec 09 '13 at 05:28
  • @Sudev Encrypted text has less redundancy than plain text and that's why it gives less compression as compared to plain text and so what you are getting is completely normal.I don't think other archive formats such as 7z,arc,rar etc would yield more satisfying results.So don't bother yourself about that.But if you're really concerned about security I suggest you to compress plain text files and then encrypt the archive. – devavx Dec 09 '13 at 06:46

0 Answers0