本文整理汇总了C#中Crc32.Update方法的典型用法代码示例。如果您正苦于以下问题:C# Crc32.Update方法的具体用法?C# Crc32.Update怎么用?C# Crc32.Update使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Crc32
的用法示例。
在下文中一共展示了Crc32.Update方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Tests
public void Tests()
{
var crc = new Crc32();
Assert.AreEqual(0, crc.Value);
crc.Update(145);
Assert.AreEqual(1426738271, crc.Value);
crc.Update(123456789);
Assert.AreEqual(1147030863, crc.Value);
byte[] data = new byte[] { 145, 234, 156 };
crc.Update(data);
Assert.AreEqual(3967437022, crc.Value);
}
示例2: 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);
}
}
}
示例3: AddStreamAsync
public async Task AddStreamAsync(string key, Stream stream)
{
var crc = new Crc32();
var useStream = stream as BufferedStream ?? new BufferedStream(stream);
byte[] buffer = new byte[0x1000];
int bytesRead;
while ((bytesRead = await useStream.ReadAsync(buffer, 0, 0x1000)) > 0)
crc.Update(buffer, 0, bytesRead);
_entries.Add(key, crc.Value);
}
示例4: Main
public static int Main(string[] args)
{
if (args.Length == 0) {
ShowHelp();
return 1;
}
var parser = new ArgumentParser(args);
if (!File.Exists(file_)) {
Console.Error.WriteLine("Cannot find file {0}", file_);
ShowHelp();
return 1;
}
using (FileStream checksumStream = File.OpenRead(file_)) {
byte[] buffer = new byte[4096];
int bytesRead;
switch (parser.Command) {
case Command.Help:
ShowHelp();
break;
case Command.Crc32:
var currentCrc = new Crc32();
while ((bytesRead = checksumStream.Read(buffer, 0, buffer.Length)) > 0) {
currentCrc.Update(buffer, 0, bytesRead);
}
Console.WriteLine("CRC32 for {0} is 0x{1:X8}", args[0], currentCrc.Value);
break;
case Command.BZip2:
var currentBZip2Crc = new BZip2Crc();
while ((bytesRead = checksumStream.Read(buffer, 0, buffer.Length)) > 0) {
currentBZip2Crc.Update(buffer, 0, bytesRead);
}
Console.WriteLine("BZip2CRC32 for {0} is 0x{1:X8}", args[0], currentBZip2Crc.Value);
break;
case Command.Adler:
var currentAdler = new Adler32();
while ((bytesRead = checksumStream.Read(buffer, 0, buffer.Length)) > 0) {
currentAdler.Update(buffer, 0, bytesRead);
}
Console.WriteLine("Adler32 for {0} is 0x{1:X8}", args[0], currentAdler.Value);
break;
}
}
return 0;
}
示例5: WriteZipFile
public static void WriteZipFile(List<FileDetails> filesToZip, string path,string manifestPath,string manifest )
{
int compression = 9;
FileDetails fd = new FileDetails(manifest, manifestPath,manifestPath);
filesToZip.Insert(0,fd);
foreach (FileDetails obj in filesToZip)
if (!File.Exists(obj.FilePath))
throw new ArgumentException(string.Format("The File {0} does not exist!", obj.FileName));
Object _locker=new Object();
lock(_locker)
{
Crc32 crc32 = new Crc32();
ZipOutputStream stream = new ZipOutputStream(File.Create(path));
stream.SetLevel(compression);
for (int i = 0; i < filesToZip.Count; i++)
{
ZipEntry entry = new ZipEntry(filesToZip[i].FolderInfo + "/" + filesToZip[i].FileName);
entry.DateTime = DateTime.Now;
if (i == 0)
{
entry = new ZipEntry(manifest);
}
using (FileStream fs = File.OpenRead(filesToZip[i].FilePath))
{
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
entry.Size = fs.Length;
fs.Close();
crc32.Reset();
crc32.Update(buffer);
entry.Crc = crc32.Value;
stream.PutNextEntry(entry);
stream.Write(buffer, 0, buffer.Length);
}
}
stream.Finish();
stream.Close();
DeleteManifest(manifestPath);
}
}
示例6: Main
public static void Main(string[] args)
{
string[] filenames = Directory.GetFiles(args[0]);
Crc32 crc = new Crc32();
ZipOutputStream s = new ZipOutputStream(File.Create(args[1]));
s.SetLevel(6); // 0 - store only to 9 - means best compression
foreach (string file in filenames) {
FileStream fs = File.OpenRead(file);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
ZipEntry entry = new ZipEntry(file);
entry.DateTime = DateTime.Now;
// set Size and the crc, because the information
// about the size and crc should be stored in the header
// if it is not set it is automatically written in the footer.
// (in this case size == crc == -1 in the header)
// Some ZIP programs have problems with zip files that don't store
// the size and crc in the header.
entry.Size = fs.Length;
fs.Close();
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
s.PutNextEntry(entry);
s.Write(buffer, 0, buffer.Length);
}
s.Finish();
s.Close();
}
示例7: ZipData
/// <summary>
/// Архивирует данные одного потока в другой поток.
/// </summary>
/// <param name="inputStream">Входной поток.</param>
/// <param name="outputStream">Выходной поток.</param>
/// <param name="entryFileName">Имя файла, которое будет помещено в выходном архиве.</param>
public static void ZipData( Stream inputStream, Stream outputStream, string entryFileName )
{
Crc32 crc = new Crc32();
ZipOutputStream zipStream = new ZipOutputStream( outputStream );
// начинаем архивировать
zipStream.SetLevel( 9 ); // уровень сжатия
long length = inputStream.Length;
byte[] buffer = new byte[length];
inputStream.Read( buffer, 0, buffer.Length );
ZipEntry entry = new ZipEntry( entryFileName );
entry.DateTime = DateTime.Now;
entry.Size = length;
crc.Reset();
crc.Update( buffer );
entry.Crc = crc.Value;
zipStream.PutNextEntry( entry );
zipStream.Write( buffer, 0, buffer.Length );
zipStream.Finish();
}
示例8: ReadHeader
void ReadHeader()
{
/* 1. Check the two magic bytes */
Crc32 headCRC = new Crc32();
int magic = baseInputStream.ReadByte();
if (magic < 0)
{
eos = true;
return;
}
headCRC.Update(magic);
if (magic != (GZipConstants.GZIP_MAGIC >> 8))
{
throw new IOException("Error baseInputStream GZIP header, first byte doesn't match");
}
magic = baseInputStream.ReadByte();
if (magic != (GZipConstants.GZIP_MAGIC & 0xFF))
{
throw new IOException("Error baseInputStream GZIP header, second byte doesn't match");
}
headCRC.Update(magic);
/* 2. Check the compression type (must be 8) */
int CM = baseInputStream.ReadByte();
if (CM != 8)
{
throw new IOException("Error baseInputStream GZIP header, data not baseInputStream deflate format");
}
headCRC.Update(CM);
/* 3. Check the flags */
int flags = baseInputStream.ReadByte();
if (flags < 0)
{
throw new Exception("Early EOF baseInputStream GZIP header");
}
headCRC.Update(flags);
/* This flag byte is divided into individual bits as follows:
bit 0 FTEXT
bit 1 FHCRC
bit 2 FEXTRA
bit 3 FNAME
bit 4 FCOMMENT
bit 5 reserved
bit 6 reserved
bit 7 reserved
*/
/* 3.1 Check the reserved bits are zero */
if ((flags & 0xd0) != 0)
{
throw new IOException("Reserved flag bits baseInputStream GZIP header != 0");
}
/* 4.-6. Skip the modification time, extra flags, and OS type */
for (int i = 0; i < 6; i++)
{
int readByte = baseInputStream.ReadByte();
if (readByte < 0)
{
throw new Exception("Early EOF baseInputStream GZIP header");
}
headCRC.Update(readByte);
}
/* 7. Read extra field */
if ((flags & GZipConstants.FEXTRA) != 0)
{
/* Skip subfield id */
for (int i = 0; i < 2; i++)
{
int readByte = baseInputStream.ReadByte();
if (readByte < 0)
{
throw new Exception("Early EOF baseInputStream GZIP header");
}
headCRC.Update(readByte);
}
if (baseInputStream.ReadByte() < 0 || baseInputStream.ReadByte() < 0)
{
throw new Exception("Early EOF baseInputStream GZIP header");
}
int len1, len2, extraLen;
len1 = baseInputStream.ReadByte();
len2 = baseInputStream.ReadByte();
if ((len1 < 0) || (len2 < 0))
{
throw new Exception("Early EOF baseInputStream GZIP header");
}
headCRC.Update(len1);
headCRC.Update(len2);
extraLen = (len1 << 8) | len2;
for (int i = 0; i < extraLen; i++)
{
//.........这里部分代码省略.........
示例9: ZipDir
/// <summary>
/// 压缩文件夹的方法
/// </summary>
/// <param name="DirToZip">被压缩的文件名称(包含文件路径)</param>
/// <param name="ZipedFile">压缩后的文件名称(包含文件路径)</param>
/// <param name="CompressionLevel">压缩率0(无压缩),9(压缩率最高)</param>
public void ZipDir(string DirToZip, string ZipedFile, int CompressionLevel)
{
//压缩文件为空时默认与压缩文件夹同一级目录
if (ZipedFile == string.Empty)
{
ZipedFile = DirToZip.Substring(DirToZip.LastIndexOf("/") + 1);
ZipedFile = DirToZip.Substring(0, DirToZip.LastIndexOf("/")) + "//" + ZipedFile + ".zip";
}
if (Path.GetExtension(ZipedFile) != ".zip")
{
ZipedFile = ZipedFile + ".zip";
}
using (ZipOutputStream zipoutputstream = new ZipOutputStream(File.Create(ZipedFile)))
{
zipoutputstream.SetLevel(CompressionLevel);
Crc32 crc = new Crc32();
Hashtable fileList = getAllFies(DirToZip);
foreach (DictionaryEntry item in fileList)
{
FileStream fs = File.OpenRead(item.Key.ToString());
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
ZipEntry entry = new ZipEntry(item.Key.ToString().Substring(DirToZip.Length));
entry.DateTime = (DateTime)item.Value;
entry.Size = fs.Length;
fs.Close();
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
zipoutputstream.PutNextEntry(entry);
zipoutputstream.Write(buffer, 0, buffer.Length);
}
}
}
示例10: ZipFileDictory
/// <summary>
/// 递归压缩文件夹方法
/// </summary>
private static bool ZipFileDictory(string FolderToZip, ZipOutputStream s, string ParentFolderName)
{
bool res = true;
string[] folders, filenames;
ZipEntry entry = null;
FileStream fs = null;
Crc32 crc = new Crc32();
try
{
entry = new ZipEntry(Path.Combine(ParentFolderName, Path.GetFileName(FolderToZip) + "/"));
s.PutNextEntry(entry);
s.Flush();
filenames = Directory.GetFiles(FolderToZip);
foreach (string file in filenames)
{
fs = File.OpenRead(file);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
entry = new ZipEntry(Path.Combine(ParentFolderName, Path.GetFileName(FolderToZip) + "/" + Path.GetFileName(file)));
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);
}
}
catch
{
res = false;
}
finally
{
if (fs != null)
{
fs.Close();
fs = null;
}
if (entry != null)
{
entry = null;
}
GC.Collect();
GC.Collect(1);
}
folders = Directory.GetDirectories(FolderToZip);
foreach (string folder in folders)
{
if (!ZipFileDictory(folder, s, Path.Combine(ParentFolderName, Path.GetFileName(FolderToZip))))
{
return false;
}
}
return res;
}
示例11: ZipSetp
/// <summary>
/// 递归遍历目录
/// </summary>
/// <param name="directory">文件夹目录</param>
/// <param name="zipStream">The ZipOutputStream Object.</param>
/// <param name="parentPath">The parent path.</param>
private static void ZipSetp(string directory, ZipOutputStream zipStream, string parentPath, IList filters = null)
{
if (directory[directory.Length - 1] != Path.DirectorySeparatorChar)
directory += Path.DirectorySeparatorChar;
Crc32 crc = new Crc32();
string[] filenames = Directory.GetFileSystemEntries(directory);
foreach (string file in filenames)
{
if (Directory.Exists(file))
{
string pPath = parentPath;
pPath += file.Substring(file.LastIndexOf("/") + 1);
pPath += "/";
ZipSetp(file, zipStream, pPath, filters);
}
else
{
string s = file.Substring(file.LastIndexOf('.'));
if (filters != null && filters.Contains(s))
continue;
using (FileStream fs = File.OpenRead(file))
{
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
string fileName = parentPath + file.Substring(file.LastIndexOf("/") + 1);
ZipEntry entry = new ZipEntry(fileName);
entry.Size = fs.Length;
fs.Close();
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
zipStream.PutNextEntry(entry);
zipStream.Write(buffer, 0, buffer.Length);
}
}
}
}
示例12: ZipCompress
/// <summary>
/// Stores incomming data as standard zip file into byte array
/// </summary>
/// <param name="fileName">Filename</param>
/// <param name="compressionLevel">Compression level 1-9</param>
/// <returns>zip file as byte array</returns>
private static byte[] ZipCompress(string fileName, int compressionLevel)
{
//SqlPipe pipe = SqlContext.Pipe;
using (MemoryStream tempFileStream = new MemoryStream())
using (ZipOutputStream zipOutput = new ZipOutputStream(tempFileStream))
{
zipOutput.SetLevel(compressionLevel);
Crc32 crc = new Crc32();
// Get local path and create stream to it.
using (FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
{
// Read full stream to in-memory buffer.
byte[] buffer = new byte[fileStream.Length];
int offset = 0;
int remaining = buffer.Length;
while (remaining > 0)
{
int read = fileStream.Read(buffer, offset, buffer.Length);
if (read <= 0)
throw new EndOfStreamException
(String.Format("End of stream reached with {0} bytes left to read", remaining));
remaining -= read;
offset += read;
}
//get crc to store in header.
crc.Reset();
crc.Update(buffer);
// Create a new entry for the current file.
ZipEntry entry = new ZipEntry(ZipEntry.CleanName(Path.GetFileName(fileName)))
{
DateTime = DateTime.Now,
Size = fileStream.Length,
Crc = crc.Value
};
fileStream.Close();
zipOutput.PutNextEntry(entry);
zipOutput.Write(buffer, 0, buffer.Length);
// Finalize the zip output.
zipOutput.Finish();
// Flushes the create and close.
zipOutput.Flush();
}
zipOutput.Close();
//retrun zip compiant data stream
return tempFileStream.ToArray();
}
}
示例13: StrangeCrcChecksum
/// <summary>Calculates the StrangeCRC check-sum on specified portion of a buffer.</summary>
/// <param name="data">Data buffer to perform check-sum on.</param>
/// <param name="startIndex">Starts index in data buffer to begin check-sum.</param>
/// <param name="length">Total number of bytes from <paramref name="startIndex">startIndex</paramref> to
/// perform check-sum over.</param>
/// <remarks>
/// The StrangeCRC is used by BZip library algorithms which are specifically used for
/// lossless, block-sorting data compression.
/// </remarks>
/// <returns>Computed StrangeCRC checksum over the specified portion of the buffer.</returns>
public static uint StrangeCrcChecksum(this byte[] data, int startIndex, int length)
{
Crc32 checksum = new Crc32();
checksum.Update(data, startIndex, length);
return checksum.Value;
}
示例14: ReadHeader
void ReadHeader()
{
// 1. Check the two magic bytes
Crc32 headCRC = new Crc32();
int magic = baseInputStream.ReadByte();
if (magic < 0) {
throw new EndOfStreamException("EOS reading GZIP header");
}
headCRC.Update(magic);
if (magic != (GZipConstants.GZIP_MAGIC >> 8)) {
throw new GZipException("Error GZIP header, first magic byte doesn't match");
}
magic = baseInputStream.ReadByte();
if (magic < 0) {
throw new EndOfStreamException("EOS reading GZIP header");
}
if (magic != (GZipConstants.GZIP_MAGIC & 0xFF)) {
throw new GZipException("Error GZIP header, second magic byte doesn't match");
}
headCRC.Update(magic);
// 2. Check the compression type (must be 8)
int compressionType = baseInputStream.ReadByte();
if ( compressionType < 0 ) {
throw new EndOfStreamException("EOS reading GZIP header");
}
if ( compressionType != 8 ) {
throw new GZipException("Error GZIP header, data not in deflate format");
}
headCRC.Update(compressionType);
// 3. Check the flags
int flags = baseInputStream.ReadByte();
if (flags < 0) {
throw new EndOfStreamException("EOS reading GZIP header");
}
headCRC.Update(flags);
/* This flag byte is divided into individual bits as follows:
bit 0 FTEXT
bit 1 FHCRC
bit 2 FEXTRA
bit 3 FNAME
bit 4 FCOMMENT
bit 5 reserved
bit 6 reserved
bit 7 reserved
*/
// 3.1 Check the reserved bits are zero
if ((flags & 0xE0) != 0) {
throw new GZipException("Reserved flag bits in GZIP header != 0");
}
// 4.-6. Skip the modification time, extra flags, and OS type
for (int i=0; i< 6; i++) {
int readByte = baseInputStream.ReadByte();
if (readByte < 0) {
throw new EndOfStreamException("EOS reading GZIP header");
}
headCRC.Update(readByte);
}
// 7. Read extra field
if ((flags & GZipConstants.FEXTRA) != 0) {
// Skip subfield id
for (int i=0; i< 2; i++) {
int readByte = baseInputStream.ReadByte();
if (readByte < 0) {
throw new EndOfStreamException("EOS reading GZIP header");
}
headCRC.Update(readByte);
}
if (baseInputStream.ReadByte() < 0 || baseInputStream.ReadByte() < 0) {
throw new EndOfStreamException("EOS reading GZIP header");
}
int len1, len2;
len1 = baseInputStream.ReadByte();
len2 = baseInputStream.ReadByte();
if ((len1 < 0) || (len2 < 0)) {
throw new EndOfStreamException("EOS reading GZIP header");
}
headCRC.Update(len1);
headCRC.Update(len2);
int extraLen = (len1 << 8) | len2;
for (int i = 0; i < extraLen;i++) {
int readByte = baseInputStream.ReadByte();
//.........这里部分代码省略.........
示例15: ZipFileFromDirectory
/// <summary>
/// 压缩目录(包括子目录及所有文件)
/// </summary>
/// <param name="rootPath">要压缩的根目录</param>
/// <param name="destinationPath">保存路径</param>
/// <param name="compressLevel">压缩程度,范围0-9,数值越大,压缩程序越高</param>
public void ZipFileFromDirectory(string rootPath, string destinationPath, int compressLevel)
{
GetAllDirectories(rootPath);
/* while (rootPath.LastIndexOf("\\") + 1 == rootPath.Length)//检查路径是否以"\"结尾
{
rootPath = rootPath.Substring(0, rootPath.Length - 1);//如果是则去掉末尾的"\"
}
*/
//string rootMark = rootPath.Substring(0, rootPath.LastIndexOf("\\") + 1);//得到当前路径的位置,以备压缩时将所压缩内容转变成相对路径。
string rootMark = rootPath + "\\";//得到当前路径的位置,以备压缩时将所压缩内容转变成相对路径。
Crc32 crc = new Crc32();
ZipOutputStream outPutStream = new ZipOutputStream(File.Create(destinationPath));
outPutStream.SetLevel(compressLevel); // 0 - store only to 9 - means best compression
foreach (string file in files)
{
FileStream fileStream = File.OpenRead(file);//打开压缩文件
byte[] buffer = new byte[fileStream.Length];
fileStream.Read(buffer, 0, buffer.Length);
ZipEntry entry = new ZipEntry(file.Replace(rootMark, string.Empty));
entry.DateTime = DateTime.Now;
// set Size and the crc, because the information
// about the size and crc should be stored in the header
// if it is not set it is automatically written in the footer.
// (in this case size == crc == -1 in the header)
// Some ZIP programs have problems with zip files that don't store
// the size and crc in the header.
entry.Size = fileStream.Length;
fileStream.Close();
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
outPutStream.PutNextEntry(entry);
outPutStream.Write(buffer, 0, buffer.Length);
}
this.files.Clear();
foreach (string emptyPath in paths)
{
ZipEntry entry = new ZipEntry(emptyPath.Replace(rootMark, string.Empty) + "/");
outPutStream.PutNextEntry(entry);
}
this.paths.Clear();
outPutStream.Finish();
outPutStream.Close();
GC.Collect();
}