C# 压缩后Sharpzip lib文件夹大小相同
我正在使用sharpzip lib压缩文件夹。但压缩后压缩大小略高于文件夹大小。 我的文件夹包括使用aes/Rsa加密的C# 压缩后Sharpzip lib文件夹大小相同,c#,sharpziplib,C#,Sharpziplib,我正在使用sharpzip lib压缩文件夹。但压缩后压缩大小略高于文件夹大小。 我的文件夹包括使用aes/Rsa加密的3个xml文件。 例如:size=1kb磁盘大小=12KB ArrayList array = new ArrayList(); int TrimLength = (Directory.GetParent(PathToFile)).ToString().Length; string test=@"\"; if (Directory.Exists(PathToFile)) {
3个xml
文件。
例如:size=1kb磁盘大小=12KB
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;
压缩后size=1.32KB
和磁盘上的大小4KB
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;
我不认为加密的东西能像纯文本那样压缩。这可能与此有关。@Sudev加密文本比纯文本具有更少的冗余,这就是为什么它比纯文本提供更少的压缩,因此您得到的是完全正常的。我不认为其他存档格式,如7z、arc、,rar etc将产生更令人满意的结果。因此,不要为此烦恼。但如果你真的关心安全性,我建议你压缩纯文本文件,然后加密存档。