本文整理汇总了C#中ICSharpCode.SharpZipLib.Zip.ZipOutputStream.Flush方法的典型用法代码示例。如果您正苦于以下问题:C# ZipOutputStream.Flush方法的具体用法?C# ZipOutputStream.Flush怎么用?C# ZipOutputStream.Flush使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ICSharpCode.SharpZipLib.Zip.ZipOutputStream
的用法示例。
在下文中一共展示了ZipOutputStream.Flush方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: WriteToZipStream
private void WriteToZipStream(ZipOutputStream zipOutputStream, IPolicySetCache set, bool runtime)
{
const int maxCompression = 9;
zipOutputStream.SetLevel(maxCompression);
AddZipEntryToZipOutputStream(zipOutputStream, WritePolicySetProperties(set, runtime), "properties.xml");
AddOnePolicySetVersionASZipEntry(set.LatestVersion, zipOutputStream, runtime);
zipOutputStream.Flush();
zipOutputStream.Finish();
}
示例2: ZipFile
/// <summary>
/// 压缩文件
/// </summary>
/// <param name="dirPath">压缩目录(只压缩PDF文件)</param>
/// <param name="zipFilePath">目标目录(如果为空,则以压缩目录为准)</param>
/// <param name="error">异常信息</param>
/// <returns></returns>
public static bool ZipFile(string dirPath, string zipFilePath, out string error)
{
error = "";
if (string.IsNullOrEmpty(dirPath) || !Directory.Exists(dirPath)) {
error = "dirPath Error";
return false;
}
if (string.IsNullOrEmpty(zipFilePath)) {
if (dirPath.EndsWith("\\")) {
dirPath = dirPath.Substring(0, dirPath.Length - 1);
}
zipFilePath = dirPath + ".zip";
}
try {
string[] fileNames = Directory.GetFiles(dirPath);
using (ZipOutputStream s = new ZipOutputStream(File.Create(zipFilePath))) {
s.SetLevel(9);
byte[] buffer = new byte[4096];
foreach (string file in fileNames) {
if (Path.GetExtension(file).ToLower().Equals(".pdf")) {
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.Flush();
s.Close();
}
} catch (Exception ex) {
error = ex.Message.ToString();
return false;
}
return true;
}
示例3: CompressImageBySharpZibLib
public void CompressImageBySharpZibLib(Stream compressStream,string compressFileName)
{
string saveZipFilePath = DateTime.Now.ToString("yyyyMMddhhmmss")+"zipfile";
using (IsolatedStorageFile isolateFile = IsolatedStorageFile.GetUserStoreForApplication())
{
using (var streamOut = new ZipOutputStream(isolateFile.OpenFile(saveZipFilePath,FileMode.Create)))
{
streamOut.SetLevel(9);
var streamIn = compressStream;
string newName = ZipEntry.CleanName(compressFileName);
ZipEntry compressEntity = new ZipEntry(newName);
compressEntity.Size = compressStream.Length;
compressEntity.DateTime = DateTime.Now;
streamOut.PutNextEntry(compressEntity);
streamIn.CopyTo(streamOut);
streamOut.Flush();
streamOut.Finish();
var convertValue = ((float)streamOut.Length / (float)streamIn.Length) * 100 + "%";
}
}
}
示例4: ZipFiles
public bool ZipFiles(string inputFolderPath, string outputPathAndFile, string password)
{
try
{
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 = inputFolderPath + @"\" + outputPathAndFile;
string outPath = outputPathAndFile;
using (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;
string file1 = "";
string file2 = "";
foreach (string file in ar) // for each file, generate a zipentry
{
oZipEntry = new ZipEntry(file.Remove(0, file.LastIndexOf('\\') + 1));
oZipStream.PutNextEntry(oZipEntry);
if (file1 == "") file1 = file; else file2 = file;
if (!file.EndsWith(@"/")) // if a file ends with '/' its a directory
{
ostream = File.OpenRead(file);
obuffer = new byte[ostream.Length];
ostream.Read(obuffer, 0, obuffer.Length);
oZipStream.Write(obuffer, 0, obuffer.Length);
oZipStream.Flush();
ostream.Flush();
ostream.Close();
}
oZipEntry = null;
}
oZipStream.Flush();
oZipStream.Finish();
oZipStream.Close();
}
}
catch (Exception ex)
{
return false;
}
return true;
}
示例5: 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;
}
示例6: btnExport_Click
private void btnExport_Click(object sender, EventArgs e)
{
string tmpf = Path.GetTempFileName() + ".zip";
using (var stream = new FileStream(tmpf,FileMode.Create,FileAccess.Write,FileShare.Read))
{
var zip = new ZipOutputStream(stream)
{
IsStreamOwner = false,
UseZip64 = UseZip64.Off
};
foreach (var entry in PSGEntries)
{
var ze = new ZipEntry(entry.name + ".wav") { CompressionMethod = CompressionMethod.Deflated };
zip.PutNextEntry(ze);
var ms = new MemoryStream();
var bw = new BinaryWriter(ms);
bw.Write(emptyWav, 0, emptyWav.Length);
ms.Position = 0x18; //samplerate and avgbytespersecond
bw.Write(20000);
bw.Write(20000 * 2);
bw.Flush();
ms.Position = 0x2C;
for (int i = 0; i < 32; i++)
for(int j=0;j<16;j++)
bw.Write(entry.waveform[i]);
bw.Flush();
var buf = ms.GetBuffer();
zip.Write(buf, 0, (int)ms.Length);
zip.Flush();
zip.CloseEntry();
}
zip.Close();
stream.Flush();
}
System.Diagnostics.Process.Start(tmpf);
}
示例7: ServerBackup
public ActionResult ServerBackup(string serverName)
{
var authres = CheckAuthorizationAndLog(Audit.AuditTypeEnum.View, true, serverName,"","", "backup server");
if (authres != null)
return authres;
DNSManagement.Server msserver = new DNSManagement.Server(serverName, Session["Username"] as string, Session["Password"] as string);
BackupModel bm = new BackupModel();
bm.PopulateFromServer(msserver);
//var res = bm.ToXml();
var files = bm.ToConfigurationFiles();
MemoryStream backupFile = new MemoryStream();
ZipOutputStream zipBackup = new ZipOutputStream(backupFile);
foreach (var file in files)
{
var data = System.Text.Encoding.UTF8.GetBytes(file.Value);
if ((data.Length == 0) || (string.IsNullOrEmpty(file.Key)))
continue;
ZipEntry entry = new ZipEntry(ZipEntry.CleanName(file.Key));
entry.DateTime = DateTime.Now;
entry.Size = data.Length;
zipBackup.PutNextEntry(entry);
zipBackup.Write(data, 0, data.Length);
zipBackup.CloseEntry();
}
zipBackup.Flush();
zipBackup.IsStreamOwner = false;
zipBackup.Close();
Response.ContentType = "application/zip";
Response.AppendHeader("content-disposition", string.Format("attachment; filename=\"Backup_{0}_{1}.zip\"", serverName, DateTime.Now.ToString("yyyyMMddHHmmss")));
Response.CacheControl = "Private";
Response.Cache.SetExpires(DateTime.Now.AddMinutes(3)); // or put a timestamp in the filename in the content-disposition
Response.OutputStream.Write(backupFile.ToArray(), 0, (int)backupFile.Length);
Response.Flush();
return new EmptyResult();
//backup all settings to xml
//backup all zones to bind files (?)
}
示例8: HandleZipOutput
static void HandleZipOutput(FileInfo zipFileInfo, string thumbprint, string pemPublicCert, byte[] cerData, string pemPrivateKey, byte[] pkcs12Data, string password)
{
var baseCn = zipFileInfo.Name;
if (baseCn.EndsWith(".zip"))
{
baseCn = baseCn.Substring(0, baseCn.Length - 4);
}
using (ZipOutputStream zipOutputStream = new ZipOutputStream(File.Create(zipFileInfo.FullName)))
{
//zipOutputStream.SetLevel(9);
zipOutputStream.UseZip64 = UseZip64.Off; // for OSX to be happy
if (cerData != null)
{
ZipEntry entry = new ZipEntry(baseCn + "/" + baseCn + "_asBytes.cer");
entry.DateTime = DateTime.Now;
zipOutputStream.PutNextEntry(entry);
byte[] buffer = new byte[4096];
using (Stream stream = new MemoryStream(cerData))
{
int sourceBytes;
do
{
sourceBytes = stream.Read(buffer, 0, buffer.Length);
zipOutputStream.Write(buffer, 0, sourceBytes);
} while (sourceBytes > 0);
}
}
if (pkcs12Data != null)
{
ZipEntry entry = new ZipEntry(baseCn + "/" + baseCn + ".pfx");
entry.DateTime = DateTime.Now;
zipOutputStream.PutNextEntry(entry);
byte[] buffer = new byte[4096];
using (Stream stream = new MemoryStream(pkcs12Data))
{
int sourceBytes;
do
{
sourceBytes = stream.Read(buffer, 0, buffer.Length);
zipOutputStream.Write(buffer, 0, sourceBytes);
} while (sourceBytes > 0);
}
}
if (!string.IsNullOrEmpty(pemPrivateKey))
{
MemoryStream ms = new MemoryStream();
StreamWriter sw = new StreamWriter(ms);
sw.Write(pemPrivateKey);
sw.Flush(); //This is required or you get a blank text file :)
ms.Position = 0;
ZipEntry entry = new ZipEntry(baseCn + "/" + baseCn + ".pem");
entry.DateTime = DateTime.Now;
zipOutputStream.PutNextEntry(entry);
zipOutputStream.Write(ms.ToArray(), 0, Convert.ToInt32(ms.Length));
}
if (!string.IsNullOrEmpty(pemPublicCert))
{
MemoryStream ms = new MemoryStream();
StreamWriter sw = new StreamWriter(ms);
sw.Write(pemPublicCert);
sw.Flush(); //This is required or you get a blank text file :)
ms.Position = 0;
ZipEntry entry = new ZipEntry(baseCn + "/" + baseCn + ".cer");
entry.DateTime = DateTime.Now;
zipOutputStream.PutNextEntry(entry);
zipOutputStream.Write(ms.ToArray(), 0, Convert.ToInt32(ms.Length));
}
if (!string.IsNullOrEmpty(thumbprint))
{
MemoryStream ms = new MemoryStream();
StreamWriter sw = new StreamWriter(ms);
sw.Write(thumbprint);
sw.Flush(); //This is required or you get a blank text file :)
ms.Position = 0;
ZipEntry entry = new ZipEntry(baseCn + "/" + baseCn + "_thumbprint.txt");
entry.DateTime = DateTime.Now;
zipOutputStream.PutNextEntry(entry);
zipOutputStream.Write(ms.ToArray(), 0, Convert.ToInt32(ms.Length));
}
if (!string.IsNullOrEmpty(password))
{
MemoryStream ms = new MemoryStream();
StreamWriter sw = new StreamWriter(ms);
sw.Write(password);
sw.Flush(); //This is required or you get a blank text file :)
ms.Position = 0;
ZipEntry entry = new ZipEntry(baseCn + "/" + baseCn + "_password.txt");
entry.DateTime = DateTime.Now;
//.........这里部分代码省略.........
示例9: CloseZipStream
} // proc CreateZipStream
private void CloseZipStream(FileWrite zipStream, ZipOutputStream zip)
{
if (zip != null)
{
zip.Flush();
zip.Dispose();
}
if (zipStream != null)
zipStream.Dispose();
} // proc CloseZipStream
示例10: Write_WZ
//.........这里部分代码省略.........
File_droidBJO.Write(Convert.ToBoolean((uint)Unit.Pos.Horizontal.X));
File_droidBJO.Write(Convert.ToBoolean((uint)Unit.Pos.Horizontal.Y));
File_droidBJO.Write(Convert.ToBoolean((uint)Unit.Pos.Altitude));
File_droidBJO.Write(Convert.ToBoolean((uint)Unit.Rotation));
switch ( Args.CompileType )
{
case sWrite_WZ_Args.enumCompileType.Multiplayer:
File_droidBJO.Write(Unit.GetBJOMultiplayerPlayerNum(Args.Multiplayer.PlayerCount));
break;
case sWrite_WZ_Args.enumCompileType.Campaign:
File_droidBJO.Write(Unit.GetBJOCampaignPlayerNum());
break;
default:
Debugger.Break();
break;
}
File_droidBJO.Write(DintZeroBytes);
}
ReturnResult.Add(Serialize_WZ_FeaturesINI(INI_feature));
if ( Args.CompileType == sWrite_WZ_Args.enumCompileType.Multiplayer )
{
ReturnResult.Add(Serialize_WZ_StructuresINI(INI_struct, Args.Multiplayer.PlayerCount));
ReturnResult.Add(Serialize_WZ_DroidsINI(INI_droid, Args.Multiplayer.PlayerCount));
ReturnResult.Add(Serialize_WZ_LabelsINI(INI_Labels, Args.Multiplayer.PlayerCount));
}
else if ( Args.CompileType == sWrite_WZ_Args.enumCompileType.Campaign )
{
ReturnResult.Add(Serialize_WZ_StructuresINI(INI_struct, -1));
ReturnResult.Add(Serialize_WZ_DroidsINI(INI_droid, -1));
ReturnResult.Add(Serialize_WZ_LabelsINI(INI_Labels, 0)); //interprets -1 players as an FMap
}
File_LEV.Flush();
File_MAP.Flush();
File_GAM.Flush();
File_featBJO.Flush();
INI_feature.File.Flush();
File_TTP.Flush();
File_structBJO.Flush();
INI_struct.File.Flush();
File_droidBJO.Flush();
INI_droid.File.Flush();
INI_Labels.File.Flush();
if ( Args.CompileType == sWrite_WZ_Args.enumCompileType.Multiplayer )
{
if ( !Args.Overwrite )
{
if ( File.Exists(Args.Path) )
{
ReturnResult.ProblemAdd("A file already exists at: " + Args.Path);
return ReturnResult;
}
}
else
{
if ( File.Exists(Args.Path) )
{
try
{
File.Delete(Args.Path);
}
catch ( Exception ex )
{
ReturnResult.ProblemAdd("Unable to delete existing file: " + ex.Message);
示例11: zipFile
/// <summary>
/// Zips a file
/// </summary>
/// <param name="filePath">Path to the source file</param>
/// <param name="tempOutputFile">temp file to store the zip</param>
/// <returns>Bool</returns>
private static bool zipFile(string filePath, string tempOutputFile)
{
if (!S3FileExists(filePath)) return false;
var file = new FileInfo(filePath);
var parent = Directory.GetParent(tempOutputFile);
if (!parent.Exists)
parent.Create();
using (var fileStreamIn = File.Create(tempOutputFile))
using (var zipStreamOut = new ZipOutputStream(fileStreamIn))
{
// Zip with highest compression.
zipStreamOut.SetLevel(9);
var crc = new Crc32();
using (var fileStream = GetS3File(filePath))
{
// Create a new entry for the current file.
var entry = new ZipEntry(file.Name) { 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.
// Reset and update the crc.
crc.Reset();
zipStreamOut.PutNextEntry(entry);
const int maxbufferSize = 1024 * 8;
// Read full stream to in-memory buffer, for larger files, only buffer 8 kBytes
var buffer = new byte[maxbufferSize];
int bytesRead;
do
{
bytesRead = fileStream.Read(buffer, 0, buffer.Length);
crc.Update(buffer, 0, bytesRead);
zipStreamOut.Write(buffer, 0, bytesRead);
} while (bytesRead > 0);
// Update entry and write to zip stream.
entry.Crc = crc.Value;
// Get rid of the buffer, because this
// is a huge impact on the memory usage.
buffer = null;
fileStream.Close();
}
// Finalize the zip output.
zipStreamOut.Finish();
// Flushes the create and close.
zipStreamOut.Flush();
zipStreamOut.Close();
}
return true;
}
示例12: ZipFileDictory
private static bool ZipFileDictory(string FolderToZip, ZipOutputStream s, string ParentFolderName)
{
bool flag = true;
ZipEntry entry = null;
FileStream stream = null;
Crc32 crc = new Crc32();
try
{
entry = new ZipEntry(Path.Combine(ParentFolderName, Path.GetFileName(FolderToZip) + "/"));
s.PutNextEntry(entry);
s.Flush();
string[] files = Directory.GetFiles(FolderToZip);
foreach (string str in files)
{
stream = File.OpenRead(str);
byte[] buffer = new byte[stream.Length];
stream.Read(buffer, 0, buffer.Length);
entry = new ZipEntry(Path.Combine(ParentFolderName, Path.GetFileName(FolderToZip) + "/" + Path.GetFileName(str)))
{
DateTime = DateTime.Now,
Size = stream.Length
};
stream.Close();
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
s.PutNextEntry(entry);
s.Write(buffer, 0, buffer.Length);
}
}
catch
{
flag = false;
}
finally
{
if (stream != null)
{
stream.Close();
stream = null;
}
if (entry != null)
{
entry = null;
}
GC.Collect();
GC.Collect(1);
}
string[] directories = Directory.GetDirectories(FolderToZip);
foreach (string str2 in directories)
{
if (!ZipFileDictory(str2, s, Path.Combine(ParentFolderName, Path.GetFileName(FolderToZip))))
{
return false;
}
}
return flag;
}
示例13: WriteMemoryToZipEntryAndFlush
public static clsResult WriteMemoryToZipEntryAndFlush(MemoryStream Memory, ZipOutputStream Stream)
{
clsResult result = new clsResult("Writing to zip stream");
try
{
Memory.WriteTo(Stream);
Memory.Flush();
Stream.Flush();
Stream.CloseEntry();
}
catch (Exception exception1)
{
ProjectData.SetProjectError(exception1);
Exception exception = exception1;
result.ProblemAdd(exception.Message);
clsResult result2 = result;
ProjectData.ClearProjectError();
return result2;
}
return result;
}
示例14: CompressFolder
/// <summary>
/// Compresses all files in the given folder using sharpziplib
///
/// http://stackoverflow.com/questions/1679986/zip-file-created-with-sharpziplib-cannot-be-opened-on-mac-os-x
///
/// </summary>
/// <param name='folder'>
/// Folder.
/// </param>
/// <param name='outputfile'>
/// name of the zip
/// </param>
public static void CompressFolder(String folder, String outputfile)
{
DirectoryInfo di = new DirectoryInfo(folder);
if (di.Exists)
{
FileInfo[] files = di.GetFiles();
using (var outStream = new FileStream(outputfile, FileMode.Create))
{
using (var zipStream = new ZipOutputStream(outStream))
{
Crc32 crc = new Crc32();
foreach (FileInfo fi in files)
{
byte[] buffer = File.ReadAllBytes(fi.FullName);
ZipEntry entry = new ZipEntry(fi.Name);
entry.DateTime = fi.LastWriteTime;
entry.Size = buffer.Length;
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
zipStream.PutNextEntry(entry);
zipStream.Write(buffer, 0, buffer.Length);
}
zipStream.Finish();
// I dont think this is required at all
zipStream.Flush();
zipStream.Close();
}
}
} else
{
throw new Exception("the folder " + folder + " does not exist");
}
}
示例15: Zip
public static void Zip(FileInfo file, byte[] key, string outputFolder, PercentChangedEventHandler percentChangedCallback)
{
if (string.IsNullOrEmpty(outputFolder))
outputFolder = file.DirectoryName;
FileStream inputStream = File.OpenRead(file.FullName);
FileStream outputStream = File.OpenWrite(outputFolder + "\\" + file.Name + CRYPTOZIP_EXTENSION);
Aes aes = new AesManaged();
byte[] keyBytes = key;
byte[] ivBytes = Encoding.UTF8.GetBytes(IV_STRING);
using (CryptoStream cryptoStream = new CryptoStream(outputStream, aes.CreateEncryptor(keyBytes, ivBytes), CryptoStreamMode.Write))
{
using (ZipOutputStream zipOutputStream = new ZipOutputStream(cryptoStream))
{
zipOutputStream.SetLevel(0);
zipOutputStream.UseZip64 = UseZip64.On;
ZipEntry zipEntry = new ZipEntry(file.Name);
zipEntry.DateTime = DateTime.Now;
zipOutputStream.PutNextEntry(zipEntry);
int read = 0;
byte[] buffer = new byte[4096];
double totalRead = 0;
double lastPercent = 0;
do
{
read = inputStream.Read(buffer, 0, buffer.Length);
zipOutputStream.Write(buffer, 0, read);
totalRead += read;
double percent = Math.Floor((totalRead / inputStream.Length) * 100);
if (percent > lastPercent)
{
lastPercent = percent;
if (percentChangedCallback != null)
percentChangedCallback(new PercentChangedEventArgs(percent));
}
} while (read == buffer.Length);
zipOutputStream.Finish();
zipOutputStream.Flush();
}
}
inputStream.Close();
outputStream.Close();
}