本文整理汇总了C#中ICSharpCode.SharpZipLib.Checksums.Crc32类的典型用法代码示例。如果您正苦于以下问题:C# Crc32类的具体用法?C# Crc32怎么用?C# Crc32使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Crc32类属于ICSharpCode.SharpZipLib.Checksums命名空间,在下文中一共展示了Crc32类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ZipDir
/// <summary>
/// 压缩文件夹
/// </summary>
/// <param name="dirToZip"></param>
/// <param name="zipedFileName"></param>
/// <param name="compressionLevel">压缩率0(无压缩)9(压缩率最高)</param>
public void ZipDir(string dirToZip, string zipedFileName, int compressionLevel = 9)
{
if (Path.GetExtension(zipedFileName) != ".zip")
{
zipedFileName = zipedFileName + ".zip";
}
using (var zipoutputstream = new ZipOutputStream(File.Create(zipedFileName)))
{
zipoutputstream.SetLevel(compressionLevel);
var crc = new Crc32();
var fileList = GetAllFies(dirToZip);
foreach (DictionaryEntry item in fileList)
{
var fs = new FileStream(item.Key.ToString(), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
var buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
// ZipEntry entry = new ZipEntry(item.Key.ToString().Substring(dirToZip.Length + 1));
var entry = new ZipEntry(Path.GetFileName(item.Key.ToString()))
{
DateTime = (DateTime) item.Value,
Size = fs.Length
};
fs.Close();
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
zipoutputstream.PutNextEntry(entry);
zipoutputstream.Write(buffer, 0, buffer.Length);
}
}
}
示例2: RuntimeInfo
public RuntimeInfo(CompressionMethod method, int compressionLevel,
int size, string password, bool getCrc)
{
this.method = method;
this.compressionLevel = compressionLevel;
this.password = password;
this.size = size;
this.random = false;
original = new byte[Size];
if (random) {
System.Random rnd = new Random();
rnd.NextBytes(original);
}
else {
for (int i = 0; i < size; ++i) {
original[i] = (byte)'A';
}
}
if (getCrc) {
Crc32 crc32 = new Crc32();
crc32.Update(original, 0, size);
crc = crc32.Value;
}
}
示例3: CreateZipFile
public void CreateZipFile(string[] straFilenames, string strOutputFilename)
{
Crc32 crc = new Crc32();
ZipOutputStream zos = new ZipOutputStream(File.Create(strOutputFilename));
zos.SetLevel(m_nCompressionLevel);
foreach (string strFileName in straFilenames)
{
FileStream fs = File.OpenRead(strFileName);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
ZipEntry entry = new ZipEntry(GetFileNameWithoutDrive(strFileName));
entry.DateTime = DateTime.Now;
entry.Size = fs.Length;
fs.Close();
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
zos.PutNextEntry(entry);
zos.Write(buffer, 0, buffer.Length);
}
zos.Finish();
zos.Close();
}
示例4: CreateFileZip
/// <summary>
/// 压缩文件
/// </summary>
/// <param name="sourceFilePath">待压缩的文件或文件夹路径</param>
/// <param name="destinationZipFilePath">保存压缩文件的文件名</param>
/// <param name="level">压缩文件等级</param>
/// <returns>返回-2说明被压缩文件已经存在,返回1说明压缩成功</returns>
public static int CreateFileZip(string sourceFilePath, string destinationZipFilePath, int level)
{
if (!Directory.Exists(destinationZipFilePath.Substring(0, destinationZipFilePath.LastIndexOf("\\"))))
{
Directory.CreateDirectory(destinationZipFilePath.Substring(0, destinationZipFilePath.LastIndexOf("\\")));
}
if (File.Exists(destinationZipFilePath))
{
return -2;
}
else
{
ZipOutputStream zipStream = new ZipOutputStream(File.Create(destinationZipFilePath));
zipStream.SetLevel(level); // 压缩级别 0-9
Crc32 crc = new Crc32();
FileStream fileStream = File.OpenRead(sourceFilePath);
byte[] buffer = new byte[fileStream.Length];
fileStream.Read(buffer, 0, buffer.Length);
string tempFile = sourceFilePath.Substring(sourceFilePath.LastIndexOf("\\") + 1);
ZipEntry entry = new ZipEntry(tempFile);
entry.DateTime = DateTime.Now;
entry.Size = fileStream.Length;
fileStream.Close();
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
zipStream.PutNextEntry(entry);
zipStream.Write(buffer, 0, buffer.Length);
zipStream.Finish();
zipStream.Close();
return 1;
}
}
示例5: zip
private void zip(string strFile, ZipOutputStream s, string staticFile)
{
if (strFile[strFile.Length - 1] != Path.DirectorySeparatorChar) strFile += Path.DirectorySeparatorChar;
Crc32 crc = new Crc32();
string[] filenames = Directory.GetFileSystemEntries(strFile);
foreach (string file in filenames)
{
if (Directory.Exists(file))
{
zip(file, s, staticFile);
}
else // 否则直接压缩文件
{
//打开压缩文件
FileStream fs = File.OpenRead(file);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
string tempfile = file.Substring(staticFile.LastIndexOf("\\") + 1);
ZipEntry entry = new ZipEntry(tempfile);
entry.DateTime = DateTime.Now;
entry.Size = fs.Length;
fs.Close();
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
s.PutNextEntry(entry);
s.Write(buffer, 0, buffer.Length);
}
}
}
示例6: Zip
public static void Zip(string strFile, string strZipFile)
{
Crc32 crc1 = new Crc32();
ZipOutputStream stream1 = new ZipOutputStream(File.Create(strZipFile));
try
{
stream1.SetLevel(6);
FileStream stream2 = File.OpenRead(strFile);
byte[] buffer1 = new byte[stream2.Length];
stream2.Read(buffer1, 0, buffer1.Length);
ZipEntry entry1 = new ZipEntry(strFile.Split(new char[] { '\\' })[strFile.Split(new char[] { '\\' }).Length - 1]);
entry1.DateTime = DateTime.Now;
entry1.Size = stream2.Length;
stream2.Close();
crc1.Reset();
crc1.Update(buffer1);
entry1.Crc = crc1.Value;
stream1.PutNextEntry(entry1);
stream1.Write(buffer1, 0, buffer1.Length);
}
catch (Exception exception1)
{
throw exception1;
}
finally
{
stream1.Finish();
stream1.Close();
stream1 = null;
crc1 = null;
}
}
示例7: CreateZipFiles
/// <summary>
/// 递归压缩文件
/// </summary>
/// <param name="sourceFilePath">待压缩的文件或文件夹路径</param>
/// <param name="zipStream">打包结果的zip文件路径(类似 D:\WorkSpace\a.zip),全路径包括文件名和.zip扩展名</param>
/// <param name="staticFile"></param>
private static void CreateZipFiles(string sourceFilePath, ZipOutputStream zipStream, string staticFile)
{
Crc32 crc = new Crc32();
string[] filesArray = Directory.GetFileSystemEntries(sourceFilePath);
foreach (string file in filesArray)
{
if (Directory.Exists(file)) //如果当前是文件夹,递归
{
CreateZipFiles(file, zipStream, staticFile);
}
else //如果是文件,开始压缩
{
FileStream fileStream = File.OpenRead(file);
byte[] buffer = new byte[fileStream.Length];
fileStream.Read(buffer, 0, buffer.Length);
string tempFile = file.Substring(staticFile.LastIndexOf("\\") + 1);
ZipEntry entry = new ZipEntry(tempFile);
entry.DateTime = DateTime.Now;
entry.Size = fileStream.Length;
fileStream.Close();
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
zipStream.PutNextEntry(entry);
zipStream.Write(buffer, 0, buffer.Length);
}
}
}
示例8: AddZip
public static string AddZip(string fileName, string zipName, ZipOutputStream s)
{
Crc32 crc = new Crc32();
try
{
FileStream fs = File.OpenRead(fileName);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
fileName = Path.GetFileName(fileName);
long fileLength = fs.Length;
fs.Close();
ZipEntry entry = new ZipEntry(zipName);
entry.DateTime = DateTime.Now;
entry.Size = fileLength;
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
s.PutNextEntry(entry);
s.Write(buffer, 0, buffer.Length);
return string.Empty;
}
catch (Exception addEx)
{
return addEx.ToString();
}
}
示例9: ZipFileDirectory
private static void ZipFileDirectory(string[] files, ZipOutputStream z)
{
FileStream fs = null;
Crc32 crc = new Crc32();
try
{
foreach(string file in files)
{
fs = File.OpenRead(file);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
string fileName = Path.GetFileName(file);
ZipEntry entry = new ZipEntry(fileName);
entry.DateTime = DateTime.Now;
entry.Size = fs.Length;
fs.Close();
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
z.PutNextEntry(entry);
z.Write(buffer, 0, buffer.Length);
}
}
finally
{
if(fs!=null)
{
fs.Close();
fs = null;
}
GC.Collect();
}
}
示例10: AddStream
public void AddStream(string fileName, Stream stream)
{
if (_zipOutputStream == null)
_zipOutputStream = new ZipOutputStream(File.Open(path, FileMode.OpenOrCreate, FileAccess.ReadWrite));
//
// Create a CRC value that identifies the file
//
var crc = new Crc32();
crc.Reset();
crc.Update((int)stream.Length);
//
// Create a Zip Entry
//
var zipEntry = new ZipEntry(fileName);
zipEntry.DateTime = DateTime.Now;
zipEntry.Size = stream.Length;
zipEntry.Crc = crc.Value;
//
// Attach the Zip Entry in ZipFile
//
_zipOutputStream.PutNextEntry(zipEntry);
Pump(stream, _zipOutputStream);
_zipOutputStream.CloseEntry();
_zipOutputStream.Flush();
}
示例11: ZipFile
//public static void ZipFile(string path, string file2Zip, string zipFileName, string zip, string bldgType)
public static void ZipFile(string path, string file2Zip, string zipFileName)
{
//MemoryStream ms = InitializeGbxml(path + file2Zip, zip, bldgType) as MemoryStream;
MemoryStream ms = InitializeGbxml(Path.Combine(path , file2Zip)) as MemoryStream;
string compressedFile =Path.Combine(path, zipFileName);
if (File.Exists(compressedFile))
{
File.Delete(compressedFile);
}
Crc32 objCrc32 = new Crc32();
ZipOutputStream strmZipOutputStream = new ZipOutputStream(File.Create(compressedFile));
strmZipOutputStream.SetLevel(9);
byte[] gbXmlBuffer = new byte[ms.Length];
ms.Read(gbXmlBuffer, 0, gbXmlBuffer.Length);
ZipEntry objZipEntry = new ZipEntry(file2Zip);
objZipEntry.DateTime = DateTime.Now;
objZipEntry.Size = ms.Length;
ms.Close();
objCrc32.Reset();
objCrc32.Update(gbXmlBuffer);
objZipEntry.Crc = objCrc32.Value;
strmZipOutputStream.PutNextEntry(objZipEntry);
strmZipOutputStream.Write(gbXmlBuffer, 0, gbXmlBuffer.Length);
strmZipOutputStream.Finish();
strmZipOutputStream.Close();
strmZipOutputStream.Dispose();
}
示例12: AddFile
/// <summary>
/// 在压缩文件中创建一个新的空文件
/// </summary>
/// <param name="fileName"></param>
/// <param name="fileSize"></param>
public void AddFile(string fileName, long fileSize)
{
crc = new Crc32();
crc.Reset();
entry = new ZipEntry(fileName);
entry.DateTime = DateTime.Now;
entry.Size = fileSize;
zipStream.PutNextEntry(entry);
}
示例13: File
/// <summary>
/// 将多个文件压缩成一个文件
/// </summary>
/// <param name="source">多个文件,采用全路径,例:e:\tmp\tmp1\DD.cs</param>
/// <param name="descZip">目标ZIP文件路径</param>
/// <param name="password">密码</param>
public void File(string[] source, string descZip, string password = null) {
ZipOutputStream outStream = new ZipOutputStream(System.IO.File.Create(descZip));
if (!password.IsNullEmpty()) outStream.Password = password;
Crc32 crc = new Crc32();
foreach (string info in source) {
File(info, crc, outStream);
}
outStream.Finish();
outStream.Close();
}
示例14: ZipOutputStream
public ZipOutputStream(Stream baseOutputStream, int bufferSize) : base(baseOutputStream, new Deflater(-1, true), bufferSize)
{
this.entries = new ArrayList();
this.crc = new Crc32();
this.defaultCompressionLevel = -1;
this.curMethod = CompressionMethod.Deflated;
this.zipComment = new byte[0];
this.crcPatchPos = -1L;
this.sizePatchPos = -1L;
this.useZip64_ = ICSharpCode.SharpZipLib.Zip.UseZip64.Dynamic;
}
示例15: AddZipEntryToZipOutputStream
internal void AddZipEntryToZipOutputStream(ZipOutputStream zipOutputStream, string content, string zipEntryName)
{
byte[] xmlBytes = m_encoding.GetBytes(content);
ZipEntry zipEntry = new ZipEntry(zipEntryName);
zipEntry.Size = xmlBytes.Length;
zipEntry.DateTime = DateTime.Now;
Crc32 crc = new Crc32();
crc.Reset();
crc.Update(xmlBytes);
zipEntry.Crc = crc.Value;
zipOutputStream.PutNextEntry(zipEntry);
zipOutputStream.Write(xmlBytes, 0, xmlBytes.Length);
zipOutputStream.CloseEntry();
}