本文整理汇总了C#中ICSharpCode.SharpZipLib.Zip.ZipOutputStream.Finish方法的典型用法代码示例。如果您正苦于以下问题:C# ZipOutputStream.Finish方法的具体用法?C# ZipOutputStream.Finish怎么用?C# ZipOutputStream.Finish使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ICSharpCode.SharpZipLib.Zip.ZipOutputStream
的用法示例。
在下文中一共展示了ZipOutputStream.Finish方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ZipFiles
/// <summary>
/// Zip all the specified files into the specified ZipFileName.
/// </summary>
public static string ZipFiles(IEnumerable<string> FilesToZip, string ZipFileName, string Password)
{
if (!File.Exists(ZipFileName))
File.Delete(ZipFileName);
ZipOutputStream Zip = new ZipOutputStream(File.Create(ZipFileName));
if (Password != "")
Zip.Password = Password;
try
{
Zip.SetLevel(5); // 0 - store only to 9 - means best compression
foreach (string FileName in FilesToZip)
{
FileStream fs = File.OpenRead(FileName);
byte[] Buffer = new byte[fs.Length];
fs.Read(Buffer, 0, Buffer.Length);
fs.Close();
ZipEntry Entry = new ZipEntry(Path.GetFileName(FileName));
Zip.PutNextEntry(Entry);
Zip.Write(Buffer, 0, Buffer.Length);
}
Zip.Finish();
Zip.Close();
return ZipFileName;
}
catch (System.Exception)
{
Zip.Finish();
Zip.Close();
File.Delete(ZipFileName);
throw;
}
}
示例2: GetZipFileName
public static string GetZipFileName(string filename)
{
string zipfile = filename.Replace (".db", ".zip");
try {
using (ZipOutputStream s = new ZipOutputStream (File.Create (zipfile))) {
s.SetLevel (9); // 0 - store only to 9 - means best compression
byte[] buffer = new byte[4096];
ZipEntry entry = new ZipEntry (filename);
entry.DateTime = DateTime.Now;
s.PutNextEntry (entry);
using (FileStream fs = File.OpenRead (filename)) {
int sourceBytes;
do {
sourceBytes = fs.Read (buffer, 0, buffer.Length);
s.Write (buffer, 0, sourceBytes);
} while (sourceBytes > 0);
}
s.Finish ();
s.Close ();
}
} catch (Exception ex) {
zipfile = filename;
}
return zipfile;
}
示例3: CompressFiles
private void CompressFiles(IEnumerable<string> filePaths, ZipOutputStream zipStream)
{
foreach (var fullFileName in filePaths)
{
var fi = new FileInfo(fullFileName);
var fileName = Path.GetFileName(fullFileName);
var newEntry = new ZipEntry(fileName) {DateTime = fi.LastWriteTime, Size = fi.Length};
// Note the zip format stores 2 second granularity
// Specifying the AESKeySize triggers AES encryption. Allowable values are 0 (off), 128 or 256.
// A password on the ZipOutputStream is required if using AES.
// newEntry.AESKeySize = 256;
// To permit the zip to be unpacked by built-in extractor in WinXP and Server2003, WinZip 8, Java, and other older code,
// you need to do one of the following: Specify UseZip64.Off, or set the Size.
// If the file may be bigger than 4GB, or you do not need WinXP built-in compatibility, you do not need either,
// but the zip will be in Zip64 format which not all utilities can understand.
// zipStream.UseZip64 = UseZip64.Off;
zipStream.UseZip64 = UseZip64.Off;
zipStream.PutNextEntry(newEntry);
// Zip the file in buffered chunks
// the "using" will close the stream even if an exception occurs
var buffer = new byte[4096];
using (var streamReader = File.OpenRead(fullFileName))
{
StreamUtils.Copy(streamReader, zipStream, buffer);
}
zipStream.CloseEntry();
}
zipStream.Finish();
}
示例4: CompressFolder
static public void CompressFolder(string aFolderName, string aFullFileOuputName, string[] ExcludedFolderNames, string[] ExcludedFileNames){
// Perform some simple parameter checking. More could be done
// like checking the target file name is ok, disk space, and lots
// of other things, but for a demo this covers some obvious traps.
if (!Directory.Exists(aFolderName)) {
Debug.Log("Cannot find directory : " + aFolderName);
return;
}
try
{
string[] exFileNames = new string[0];
string[] exFolderNames = new string[0];
if(ExcludedFileNames != null) exFileNames = ExcludedFileNames;
if(ExcludedFolderNames != null) exFolderNames = ExcludedFolderNames;
// Depending on the directory this could be very large and would require more attention
// in a commercial package.
List<string> filenames = GenerateFolderFileList(aFolderName, null);
//foreach(string filename in filenames) Debug.Log(filename);
// '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 zipOut = new ZipOutputStream(File.Create(aFullFileOuputName))){
zipOut.Finish();
zipOut.Close();
}
using(ZipFile s = new ZipFile(aFullFileOuputName)){
s.BeginUpdate();
int counter = 0;
//add the file to the zip file
foreach(string filename in filenames){
bool include = true;
string entryName = filename.Replace(aFolderName, "");
//Debug.Log(entryName);
foreach(string fn in exFolderNames){
Regex regEx = new Regex(@"^" + fn.Replace(".",@"\."));
if(regEx.IsMatch(entryName)) include = false;
}
foreach(string fn in exFileNames){
Regex regEx = new Regex(@"^" + fn.Replace(".",@"\."));
if(regEx.IsMatch(entryName)) include = false;
}
if(include){
s.Add(filename, entryName);
}
counter++;
}
//commit the update once we are done
s.CommitUpdate();
//close the file
s.Close();
}
}
catch(Exception ex)
{
Debug.Log("Exception during processing" + ex.Message);
// No need to rethrow the exception as for our purposes its handled.
}
}
示例5: CreateFxz
public void CreateFxz()
{
using (var zipStream =
new ZipOutputStream(File.Create(GetTemporaryDirectory() +
"foo.fxz"))) {
_zipStream = zipStream;
zipStream.SetLevel(9);
string filename = "content.fxd";
string fullpath = GetFullPath(filename);
var ms = File.Create(fullpath);
_streamWriter = new StreamWriter(ms);
WriteContent();
ms.Close();
AddFileToZip(fullpath);
RemoveTemporaryFile(fullpath);
_streamWriter.Flush();
zipStream.Finish();
zipStream.Close();
}
}
示例6: CreateFileBundle
/// <summary>
/// Creates a bundle containing version list delta and all data of files.
/// </summary>
/// <returns>The binary bundle.</returns>
/// <param name="list">List needed to transferred.</param>
public static byte[] CreateFileBundle(List<FileEvent> list)
{
using (MemoryStream ms = new MemoryStream())
using (ZipOutputStream zip = new ZipOutputStream(ms))
{
ZipEntry block = new ZipEntry("vs");
zip.PutNextEntry(block);
zip.WriteAllBytes(list.SerializeAsBytes());
zip.CloseEntry();
foreach (var sha1 in list.Where(x => x.SHA1 != null).Select(x => x.SHA1).Distinct())
{
block = new ZipEntry(sha1);
zip.PutNextEntry(block);
zip.WriteAllBytes(File.ReadAllBytes(Config.MetaFolderData.File(sha1)));
zip.CloseEntry();
}
zip.Finish();
ms.Flush();
ms.Position = 0;
return ms.ToArray();
}
}
示例7: ZipFiles
/// <summary>
/// Método que faz o zip de arquivos encontrados no diretório <strPathDirectory>
/// </summary>
/// <param name="strPath"></param>
public static void ZipFiles(String strPathDirectory, String strZipName)
{
try
{
using (ZipOutputStream ZipOut = new ZipOutputStream(File.Create(strPathDirectory + "\\" + strZipName + ".zip")))
{
string[] OLfiles = Directory.GetFiles(strPathDirectory);
Console.WriteLine(OLfiles.Length);
ZipOut.SetLevel(9);
byte[] buffer = new byte[4096];
foreach (string filename in OLfiles)
{
ZipEntry entry = new ZipEntry(Path.GetFileName(filename));
ZipOut.PutNextEntry(entry);
using (FileStream fs = File.OpenRead(filename))
{
int sourceBytes;
do
{
sourceBytes = fs.Read(buffer, 0, buffer.Length);
ZipOut.Write(buffer, 0, sourceBytes);
} while (sourceBytes > 0);
}
}
ZipOut.Finish();
ZipOut.Close();
}
}
catch (System.Exception ex)
{
System.Console.Error.WriteLine("exception: " + ex);
//TODO colocar log
}
}
示例8: CompressFiles
public static void CompressFiles(IEnumerable<ISong> files, string destinationPath)
{
if (log.IsDebugEnabled)
{
log.Debug("Starting creation of zip file : " + destinationPath);
}
using (ZipOutputStream zipOutputStream = new ZipOutputStream(new FileStream(destinationPath, FileMode.OpenOrCreate)))
{
zipOutputStream.SetLevel(0);
foreach (ISong song in files)
{
FileInfo fileInfo = new FileInfo(song.MediaFilePath);
ZipEntry entry = new ZipEntry(song.Artist.Name + "\\" + song.Album.Name + "\\" + song.Title + fileInfo.Extension);
zipOutputStream.PutNextEntry(entry);
FileStream fs = File.OpenRead(song.MediaFilePath);
byte[] buff = new byte[1024];
int n = 0;
while ((n = fs.Read(buff, 0, buff.Length)) > 0)
{
zipOutputStream.Write(buff, 0, n);
}
fs.Close();
}
zipOutputStream.Finish();
}
if (log.IsDebugEnabled)
{
log.Debug("Zip file created : " + destinationPath);
}
}
示例9: CreateFromDirectory
public static void CreateFromDirectory(string[] sourceFileNames, string destinationArchiveFileName)
{
using (ZipOutputStream zipStream = new ZipOutputStream(File.Create(destinationArchiveFileName)))
{
byte[] buffer = new byte[BufferSize];
zipStream.SetLevel(9);
foreach (string file in sourceFileNames)
{
var entryName = Path.GetFileName(file);
var fileInfo = new FileInfo(file);
ZipEntry entry = new ZipEntry(entryName);
entry.DateTime = fileInfo.LastWriteTime;
zipStream.PutNextEntry(entry);
using (FileStream fileStream = File.OpenRead(file))
{
while (true)
{
int size = fileStream.Read(buffer, 0, buffer.Length);
if (size <= 0)
break;
zipStream.Write(buffer, 0, size);
}
}
}
zipStream.Finish();
zipStream.Close();
}
}
示例10: ZipFile
public void ZipFile(string FileToZip, string ZipedFile, int CompressionLevel, int BlockSize)
{
//如果文件没有找到,则报错
if (!System.IO.File.Exists(FileToZip))
{
throw new System.IO.FileNotFoundException("The specified file " + FileToZip + " could not be found. Zipping aborderd");
}
System.IO.FileStream StreamToZip = new System.IO.FileStream(FileToZip, System.IO.FileMode.Open, System.IO.FileAccess.Read);
System.IO.FileStream ZipFile = System.IO.File.Create(ZipedFile);
ZipOutputStream ZipStream = new ZipOutputStream(ZipFile);
ZipEntry ZipEntry = new ZipEntry("ZippedFile");
ZipStream.PutNextEntry(ZipEntry);
ZipStream.SetLevel(CompressionLevel);
byte[] buffer = new byte[BlockSize];
System.Int32 size = StreamToZip.Read(buffer, 0, buffer.Length);
ZipStream.Write(buffer, 0, size);
try
{
while (size < StreamToZip.Length)
{
int sizeRead = StreamToZip.Read(buffer, 0, buffer.Length);
ZipStream.Write(buffer, 0, sizeRead);
size += sizeRead;
}
}
catch (System.Exception ex)
{
throw ex;
}
ZipStream.Finish();
ZipStream.Close();
StreamToZip.Close();
}
示例11: AddEntryAfterFinish
public void AddEntryAfterFinish()
{
MemoryStream ms = new MemoryStream();
ZipOutputStream s = new ZipOutputStream(ms);
s.Finish();
s.PutNextEntry(new ZipEntry("dummyfile.tst"));
}
示例12: 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();
}
示例13: ZipToFile
/// <summary>
/// Zips the files in the specifed directory and outputs the zip file to the specified location.
/// </summary>
/// <param name="zipFilePath">The full path to the output zip file.</param>
/// <returns>The total number of files added to the zip file.</returns>
public int ZipToFile(string zipFilePath)
{
int total = 0;
if (Directory.Exists(DirectoryPath))
{
if (!Directory.Exists(Path.GetDirectoryName(zipFilePath)))
{
Directory.CreateDirectory(Path.GetDirectoryName(zipFilePath));
}
// Create the zip file
//Crc32 crc = new Crc32();
ZipOutputStream zipFile = new ZipOutputStream(File.Create(zipFilePath));
zipFile.UseZip64 = UseZip64.Off;
zipFile.SetLevel(9);
total += ZipFromPath(zipFile, DirectoryPath);
// Close the writer
zipFile.Finish();
zipFile.Close();
}
return total;
}
示例14: ZipFiles
public static void ZipFiles(string inputFolderPath, string outputPathAndFile, string password)
{
ArrayList ar = GenerateFileList(inputFolderPath); // generate file list
int TrimLength = (Directory.GetParent(inputFolderPath)).ToString().Length;
// find number of chars to remove // from orginal file path
TrimLength += 1; //remove '\'
FileStream ostream;
byte[] obuffer;
string outPath = outputPathAndFile;
ZipOutputStream oZipStream = new ZipOutputStream(File.Create(outPath)); // create zip stream
if (password != null && password != String.Empty)
oZipStream.Password = password;
oZipStream.SetLevel(9); // maximum compression
ZipEntry oZipEntry;
foreach (string Fil in ar) // for each file, generate a zipentry
{
oZipEntry = new ZipEntry(Fil.Remove(0, TrimLength));
oZipStream.PutNextEntry(oZipEntry);
if (!Fil.EndsWith(@"/")) // if a file ends with '/' its a directory
{
ostream = File.OpenRead(Fil);
obuffer = new byte[ostream.Length];
ostream.Read(obuffer, 0, obuffer.Length);
oZipStream.Write(obuffer, 0, obuffer.Length);
}
}
oZipStream.Finish();
oZipStream.Close();
oZipStream.Dispose();
}
示例15: CreateZipFile
public static void CreateZipFile(string[] filenames, string outputFile)
{
// Zip up the files - From SharpZipLib Demo Code
using (ZipOutputStream s = new ZipOutputStream(File.Create(outputFile)))
{
s.SetLevel(9); // 0-9, 9 being the highest level of compression
byte[] buffer = new byte[4096];
foreach (string file in filenames)
{
ZipEntry entry = new ZipEntry(Path.GetFileName(file));
entry.DateTime = DateTime.Now;
s.PutNextEntry(entry);
using (FileStream fs = File.OpenRead(file))
{
int sourceBytes;
do
{
sourceBytes = fs.Read(buffer, 0, buffer.Length);
s.Write(buffer, 0, sourceBytes);
}
while (sourceBytes > 0);
}
}
s.Finish();
s.Close();
}
}