62,243
社区成员




public static string ZipFiles(string path, string zipFileName)
{
string fileName = string.Empty;
try
{
// Depending on the directory this could be very large and would require more attention
// in a commercial package.
string[] filenames = Directory.GetFiles(path);
fileName = Path.Combine(path, zipFileName + ".zip");
// 'using' statements guarantee the stream is closed properly which is a big source
// of problems otherwise. Its exception safe as well which is great.
using (ZipOutputStream s = new ZipOutputStream(File.Create(fileName)))
{
s.UseZip64 = UseZip64.Off;
s.SetLevel(9); // 0 - store only to 9 - means best compression
byte[] buffer = new byte[4096];
foreach (string file in filenames)
{
// Using GetFileName makes the result compatible with XP
// as the resulting path is not absolute.
ZipEntry entry = new ZipEntry(Path.GetFileName(file));
entry.IsUnicodeText = true;
// Setup the entry data as required.
// Crc and size are handled by the library for seakable streams
// so no need to do them here.
// Could also use the last write time or similar for the file.
entry.DateTime = DateTime.Now;
s.PutNextEntry(entry);
using (FileStream fs = File.OpenRead(file))
{
// Using a fixed size buffer here makes no noticeable difference for output
// but keeps a lid on memory usage.
int sourceBytes;
do
{
sourceBytes = fs.Read(buffer, 0, buffer.Length);
s.Write(buffer, 0, sourceBytes);
} while (sourceBytes > 0);
}
}
// Finish is important to ensure trailing information for a Zip file is appended. Without this
// the created file would be invalid.
s.Finish();
}
}
catch (Exception)
{
// No need to rethrow the exception as for our purposes its handled.
}
return fileName;
}
public static string ZipFiles(string path, string zipFileName)
{
Logger.Debug(typeof(CompressionMgr), "ZipFiles");
string fileName = string.Empty;
try
{
// Depending on the directory this could be very large and would require more attention
// in a commercial package.
string[] fileNames = Directory.GetFiles(path);
fileName = Path.Combine(path, zipFileName + ".zip");
using (ZipFile zip = new ZipFile(Encoding.Default))
{
foreach (string file in fileNames)
{
zip.AddFile(file, "");
}
zip.Save(fileName);
}
}
catch (Exception)
{
// No need to rethrow the exception as for our purposes its handled.
}
return fileName;
}
public static string ZipFiles(string path, string zipFileName)
{
string fileName = string.Empty;
try
{
// Depending on the directory this could be very large and would require more attention
// in a commercial package.
string[] filenames = Directory.GetFiles(path);
fileName = Path.Combine(path, zipFileName + ".zip");
// 'using' statements guarantee the stream is closed properly which is a big source
// of problems otherwise. Its exception safe as well which is great.
using (ZipOutputStream s = new ZipOutputStream(File.Create(fileName)))
{
s.AlternateEncoding = Encoding.UTF8;
s.AlternateEncodingUsage = ZipOption.AsNecessary;
s.EnableZip64 = Zip64Option.Never;
s.CompressionLevel = CompressionLevel.Level9; // 0 - store only to 9 - means best compression
byte[] buffer = new byte[4096];
foreach (string file in filenames)
{
// Using GetFileName makes the result compatible with XP
// as the resulting path is not absolute.
s.PutNextEntry(Path.GetFileName(file));
using (FileStream fs = File.OpenRead(file))
{
// Using a fixed size buffer here makes no noticeable difference for output
// but keeps a lid on memory usage.
int sourceBytes;
do
{
sourceBytes = fs.Read(buffer, 0, buffer.Length);
s.Write(buffer, 0, sourceBytes);//write bytes
} while (sourceBytes > 0);
}
}
}
}
catch (Exception)
{
}
return fileName;
}