问题描述
大家好,
我正在编写压缩目录的应用程序。有人可以给我一个使用7z压缩目录的工作样本。 SevenZIp只。 plzzzz
到目前为止我有这个:
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;
}
else
{
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);
}
}
推荐答案
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;
}
else
{
szc.CompressionMode = SevenZip.CompressionMode.Create;
}
FileStream archive = new FileStream(output_stringBuilder, FileMode.OpenOrCreate, FileAccess.ReadWrite);
try
{
<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(例外e)
{
Console.WriteLine(ERROR ::::+ e.Message);
Console.WriteLine(ERROR ::::+ e.StackTrace);
}
//archive.Flush();
archive.Close();
}
thans
szc.CompressDirectory(outFile, archive);
}
catch (Exception e)
{
Console.WriteLine("ERROR :::: "+ e.Message);
Console.WriteLine("ERROR :::: " + e.StackTrace);
}
//archive.Flush();
archive.Close();
}
thans
这篇关于使用sevenzip sharp压缩目录。请帮忙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!