本文整理汇总了C#中Crc32.Reset方法的典型用法代码示例。如果您正苦于以下问题:C# Crc32.Reset方法的具体用法?C# Crc32.Reset怎么用?C# Crc32.Reset使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Crc32
的用法示例。
在下文中一共展示了Crc32.Reset方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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);
}
}
}
示例2: 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);
}
}
示例3: 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();
}
示例4: 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();
}
示例5: 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);
}
}
}
示例6: 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);
}
}
}
}
示例7: 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();
}
}
示例8: 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();
}
示例9: CreateZip
/// <summary>
/// Main method
/// </summary>
/// <param name="stZipPath">path of the archive wanted</param>
/// <param name="stDirToZip">path of the directory we want to create, without ending backslash</param>
public static void CreateZip(string stZipPath, string stDirToZip)
{
try
{
//Sanitize inputs
stDirToZip = Path.GetFullPath(stDirToZip);
stZipPath = Path.GetFullPath(stZipPath);
TextLogger2.Log("Zip directory " + stDirToZip);
//Recursively parse the directory to zip
Stack<FileInfo> stackFiles = DirExplore(stDirToZip);
ZipOutputStream zipOutput = null;
if (File.Exists(stZipPath))
File.Delete(stZipPath);
Crc32 crc = new Crc32();
zipOutput = new ZipOutputStream(File.Create(stZipPath));
zipOutput.SetLevel(6); // 0 - store only to 9 - means best compression
TextLogger2.Log(stackFiles.Count + " files to zip.\n");
int index = 0;
foreach (FileInfo fi in stackFiles)
{
++index;
//int percent = (int)((float)index / ((float)stackFiles.Count / 100));
//if (percent % 1 == 0)
//{
// Console.CursorLeft = 0;
// Console.Write(_stSchon[index % _stSchon.Length].ToString() + " " + percent + "% done.");
//}
FileStream fs = File.OpenRead(fi.FullName);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
//Create the right arborescence within the archive
string stFileName = fi.FullName.Remove(0, stDirToZip.Length + 1);
ZipEntry entry = new ZipEntry(stFileName);
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;
zipOutput.PutNextEntry(entry);
zipOutput.Write(buffer, 0, buffer.Length);
}
zipOutput.Finish();
zipOutput.Close();
zipOutput = null;
}
catch (Exception)
{
throw;
}
}
示例10: Compress
public static void Compress(string path, ZipOutputStream output, string relativePath)
{
if (!string.IsNullOrEmpty(relativePath) && !relativePath.EndsWith("\\"))
{
relativePath += "\\";
}
if (Directory.Exists(path))
{
FileSystemInfo[] fsis = new DirectoryInfo(path).GetFileSystemInfos();
ZipEntry entry = new ZipEntry(relativePath + Path.GetFileName(path) + "/");
entry.DateTime = DateTime.Now;
output.PutNextEntry(entry);
foreach (FileSystemInfo fsi in fsis)
{
Compress(path + "\\" + fsi.Name, output, relativePath + Path.GetFileName(path));
}
}
else
{
Crc32 crc = new Crc32();
//打开压缩文件
Stream fs = File.Open(path, FileMode.Open, FileAccess.Read);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
ZipEntry entry = new ZipEntry(relativePath + Path.GetFileName(path));
entry.DateTime = DateTime.Now;
fs.Close();
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
output.PutNextEntry(entry);
output.Write(buffer, 0, buffer.Length);
}
}
示例11: UpdateSettingConf
void UpdateSettingConf(string path)
{
byte[] setting_buf = CreateSettingConf();
using (ZipInputStream inputStream = new ZipInputStream(File.Open(path, FileMode.Open)))
{
using (ZipOutputStream outputStream = new ZipOutputStream(File.Create(path + ".temp")))
{
ZipEntry entry;
try
{
while ((entry = inputStream.GetNextEntry()) != null)
{
outputStream.SetLevel(6);
if (entry.Name.IndexOf("Setting.conf", StringComparison.CurrentCultureIgnoreCase) >= 0)
{
Crc32 crc = new Crc32();
//打开压缩文件
ZipEntry new_entry = new ZipEntry(entry.Name);
new_entry.DateTime = DateTime.Now;
crc.Reset();
crc.Update(setting_buf);
entry.Crc = crc.Value;
outputStream.PutNextEntry(new_entry);
outputStream.Write(setting_buf, 0, setting_buf.Length);
}
else
{
if (entry.IsDirectory)
{
ZipEntry new_entry = new ZipEntry(entry.Name);
new_entry.DateTime = DateTime.Now;
outputStream.PutNextEntry(entry);
}
else
{
byte[] buffer = new byte[entry.Size];
inputStream.Read(buffer, 0, buffer.Length);
Crc32 crc = new Crc32();
//打开压缩文件
ZipEntry new_entry = new ZipEntry(entry.Name);
new_entry.DateTime = DateTime.Now;
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
outputStream.PutNextEntry(new_entry);
outputStream.Write(buffer, 0, buffer.Length);
}
}
}
}
finally
{
outputStream.Finish();
outputStream.Close();
}
}
}
File.Copy(path + ".temp", path, true);
File.Delete(path + ".temp");
}
示例12: 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;
}