本文整理汇总了C#中ICSharpCode.SharpZipLib.Zip.ZipFile.Add方法的典型用法代码示例。如果您正苦于以下问题:C# ZipFile.Add方法的具体用法?C# ZipFile.Add怎么用?C# ZipFile.Add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ICSharpCode.SharpZipLib.Zip.ZipFile
的用法示例。
在下文中一共展示了ZipFile.Add方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TryDeleting
void TryDeleting(byte[] master, int totalEntries, int additions, params int[] toDelete)
{
MemoryStream ms = new MemoryStream();
ms.Write(master, 0, master.Length);
using (ZipFile f = new ZipFile(ms)) {
f.IsStreamOwner = false;
Assert.AreEqual(totalEntries, f.Count);
Assert.IsTrue(f.TestArchive(true));
f.BeginUpdate(new MemoryArchiveStorage());
for (int i = 0; i < additions; ++i) {
f.Add(new StringMemoryDataSource("Another great file"),
string.Format("Add{0}.dat", i + 1));
}
foreach (int i in toDelete) {
f.Delete(f[i]);
}
f.CommitUpdate();
/* write stream to file to assist debugging.
byte[] data = ms.ToArray();
using ( FileStream fs = File.Open(@"c:\aha.zip", FileMode.Create, FileAccess.ReadWrite, FileShare.Read) ) {
fs.Write(data, 0, data.Length);
}
*/
int newTotal = totalEntries + additions - toDelete.Length;
Assert.AreEqual(newTotal, f.Count,
string.Format("Expected {0} entries after update found {1}", newTotal, f.Count));
Assert.IsTrue(f.TestArchive(true), "Archive test should pass");
}
}
示例2: GetFilesToZip
/// <summary>
/// Iterate thru all the filesysteminfo objects and add it to our zip file
/// </summary>
/// <param name="fileSystemInfosToZip">a collection of files/directores</param>
/// <param name="z">our existing ZipFile object</param>
private static void GetFilesToZip(FileSystemInfo[] fileSystemInfosToZip, ZipFile z)
{
//check whether the objects are null
if (fileSystemInfosToZip != null && z != null)
{
//iterate thru all the filesystem info objects
foreach (FileSystemInfo fi in fileSystemInfosToZip)
{
//check if it is a directory
if (fi is DirectoryInfo)
{
DirectoryInfo di = (DirectoryInfo)fi;
//add the directory
z.AddDirectory(di.FullName);
//drill thru the directory to get all
//the files and folders inside it.
GetFilesToZip(di.GetFileSystemInfos(), z);
}
else
{
//add it
z.Add(fi.FullName);
}
}
}
}
示例3: AddToZip
/// <summary>
/// Add files to an existing Zip archive,
/// </summary>
/// <param name="filename">Array of path / filenames to add to the archive</param>
/// <param name="archive">Zip archive that we want to add the file to</param>
public void AddToZip(string[] filename, string archive)
{
if (!File.Exists(archive))
{
return;
}
try
{
ZipFile zf = new ZipFile(archive);
zf.BeginUpdate();
// path relative to the archive
zf.NameTransform = new ZipNameTransform(Path.GetDirectoryName(archive));
foreach (var file in filename)
{
// skip if this isn't a real file
if (!File.Exists(file))
{
continue;
}
zf.Add(file, CompressionMethod.Deflated);
}
zf.CommitUpdate();
zf.Close();
}
catch (Exception e)
{
if (e.Message != null)
{
var msg = new[] { e.Message };
LocDB.Message("defErrMsg", e.Message, msg, LocDB.MessageTypes.Error, LocDB.MessageDefault.First);
}
}
}
示例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: CompressDataInToFile
public void CompressDataInToFile(string directory, string password, string outputFile)
{
var fullFileListing = Directory.EnumerateFiles(directory, "*.*", SearchOption.AllDirectories);
var directories = Directory.EnumerateDirectories(directory, "*", SearchOption.AllDirectories);
_logger.Information("Creating ZIP File");
using (var zip = new ZipFile(outputFile))
{
zip.UseZip64 = UseZip64.On;
_logger.Information("Adding directories..");
foreach (var childDirectory in directories)
{
_logger.Information(string.Format("Adding {0}", childDirectory.Replace(directory, string.Empty)));
zip.BeginUpdate();
zip.AddDirectory(childDirectory.Replace(directory, string.Empty));
zip.CommitUpdate();
}
_logger.Information("Adding files..");
foreach (var file in fullFileListing)
{
_logger.Information(string.Format("Adding {0}", file.Replace(directory, string.Empty)));
zip.BeginUpdate();
zip.Add(file, file.Replace(directory, string.Empty));
zip.CommitUpdate();
}
_logger.Information("Setting password..");
zip.BeginUpdate();
zip.Password = password;
zip.CommitUpdate();
}
}
示例6: AddFile
/// <summary>
/// 添加单个文件到压缩包中
/// </summary>
/// <param name="filePath">要压缩的文件</param>
/// <param name="zipPath">目标压缩包路径</param>
/// <param name="filePathInZip">在压缩包中文件的路径</param>
public void AddFile(string filePath, string zipPath,string filePathInZip)
{
using (ZipFile zip = new ZipFile(zipPath))
{
zip.BeginUpdate();
zip.Add(filePath, filePathInZip);
zip.CommitUpdate();
}
}
示例7: Process
public static void Process( ZipFile zip, ChartRenderingJob chart)
{
TemporaryDataSource tds = new TemporaryDataSource(){ ms = new MemoryStream()};
var currentEntry = zip.GetEntry(chart.TemplatePath);
using( var input = zip.GetInputStream(currentEntry))
{
ChartWriter.RenderChart(chart,input,tds.ms);
}
zip.BeginUpdate();
zip.Add(tds, currentEntry.Name,currentEntry.CompressionMethod,currentEntry.IsUnicodeText);
zip.CommitUpdate();
}
示例8: CompressFile
private static long CompressFile(FileInfo fi)
{
long w = 0;
if (!overwrite && File.Exists(fi.Name.Replace(fi.Extension, ".zip")))
{
if (!quiet)
Print(String.Format("\r !! Skipping extant file {0}", fi.Name), ConsoleColor.DarkCyan);
return -1;
}
using (Stream z = File.Open(fi.Name.Replace(fi.Extension, ".zip"), FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
using (ZipFile zip = new ZipFile(z))
{
zip.BeginUpdate();
zip.Add(fi.Name);
zip.CommitUpdate();
w = z.Length;
zip.Close();
}
return w;
}
}
示例9: LogAsFile
/// <summary>
/// Saves a (looong) string of data as a daily zip file entry.
/// </summary>
/// <param name="fileContents">The data as string. NOT filename, sorry</param>
/// <param name="filenameInZip">What the file will be called in the zip file</param>
public static void LogAsFile(string fileContents, string filenameInZip)
{
string filename = LOG_DIR + CurrFilename() + ".zip";
if (!File.Exists(filename))
{
using (FileStream newzip = File.OpenWrite(filename))
{
newzip.Write(EMPTY_ZIP, 0, EMPTY_ZIP.Length);
}
}
using (ZipFile zipfile = new ZipFile(filename))
{
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(fileContents));
LogDataSource lds = new LogDataSource(ms);
zipfile.BeginUpdate();
zipfile.Add(lds, filenameInZip, CompressionMethod.Deflated, false);
zipfile.CommitUpdate();
}
}
示例10: NameFactory
public void NameFactory()
{
MemoryStream memStream = new MemoryStream();
DateTime fixedTime = new DateTime(1981, 4, 3);
using (ZipFile f = new ZipFile(memStream))
{
f.IsStreamOwner = false;
((ZipEntryFactory)f.EntryFactory).IsUnicodeText = true;
((ZipEntryFactory)f.EntryFactory).Setting = ZipEntryFactory.TimeSetting.Fixed;
((ZipEntryFactory)f.EntryFactory).FixedDateTime = fixedTime;
((ZipEntryFactory)f.EntryFactory).SetAttributes = 1;
f.BeginUpdate(new MemoryArchiveStorage());
string[] names = new string[]
{
"\u030A\u03B0", // Greek
"\u0680\u0685", // Arabic
};
foreach (string name in names)
{
f.Add(new StringMemoryDataSource("Hello world"), name,
CompressionMethod.Deflated, true);
}
f.CommitUpdate();
Assert.IsTrue(f.TestArchive(true));
foreach (string name in names)
{
int index = f.FindEntry(name, true);
Assert.IsTrue(index >= 0);
ZipEntry found = f[index];
Assert.AreEqual(name, found.Name);
Assert.IsTrue(found.IsUnicodeText);
Assert.AreEqual(fixedTime, found.DateTime);
Assert.IsTrue(found.IsDOSEntry);
}
}
}
示例11: BasicEncryption
public void BasicEncryption()
{
const string TestValue = "0001000";
MemoryStream memStream = new MemoryStream();
using (ZipFile f = new ZipFile(memStream)) {
f.IsStreamOwner = false;
f.Password = "Hello";
StringMemoryDataSource m = new StringMemoryDataSource(TestValue);
f.BeginUpdate(new MemoryArchiveStorage());
f.Add(m, "a.dat");
f.CommitUpdate();
Assert.IsTrue(f.TestArchive(true), "Archive test should pass");
}
using (ZipFile g = new ZipFile(memStream)) {
g.Password = "Hello";
ZipEntry ze = g[0];
Assert.IsTrue(ze.IsCrypted, "Entry should be encrypted");
using (StreamReader r = new StreamReader(g.GetInputStream(0))) {
string data = r.ReadToEnd();
Assert.AreEqual(TestValue, data);
}
}
}
示例12: AddEncryptedEntriesToExistingArchive
public void AddEncryptedEntriesToExistingArchive()
{
const string TestValue = "0001000";
MemoryStream memStream = new MemoryStream();
using (ZipFile f = new ZipFile(memStream)) {
f.IsStreamOwner = false;
f.UseZip64 = UseZip64.Off;
StringMemoryDataSource m = new StringMemoryDataSource(TestValue);
f.BeginUpdate(new MemoryArchiveStorage());
f.Add(m, "a.dat");
f.CommitUpdate();
Assert.IsTrue(f.TestArchive(true), "Archive test should pass");
}
using (ZipFile g = new ZipFile(memStream)) {
ZipEntry ze = g[0];
Assert.IsFalse(ze.IsCrypted, "Entry should NOT be encrypted");
using (StreamReader r = new StreamReader(g.GetInputStream(0))) {
string data = r.ReadToEnd();
Assert.AreEqual(TestValue, data);
}
StringMemoryDataSource n = new StringMemoryDataSource(TestValue);
g.Password = "Axolotyl";
g.UseZip64 = UseZip64.Off;
g.IsStreamOwner = false;
g.BeginUpdate();
g.Add(n, "a1.dat");
g.CommitUpdate();
Assert.IsTrue(g.TestArchive(true), "Archive test should pass");
ze = g[1];
Assert.IsTrue(ze.IsCrypted, "New entry should be encrypted");
using (StreamReader r = new StreamReader(g.GetInputStream(0))) {
string data = r.ReadToEnd();
Assert.AreEqual(TestValue, data);
}
}
}
示例13: AddAndDeleteEntriesMemory
public void AddAndDeleteEntriesMemory()
{
MemoryStream memStream = new MemoryStream();
using (ZipFile f = new ZipFile(memStream)) {
f.IsStreamOwner = false;
f.BeginUpdate(new MemoryArchiveStorage());
f.Add(new StringMemoryDataSource("Hello world"), @"z:\a\a.dat");
f.Add(new StringMemoryDataSource("Another"), @"\b\b.dat");
f.Add(new StringMemoryDataSource("Mr C"), @"c\c.dat");
f.Add(new StringMemoryDataSource("Mrs D was a star"), @"d\d.dat");
f.CommitUpdate();
Assert.IsTrue(f.TestArchive(true));
}
byte[] master = memStream.ToArray();
TryDeleting(master, 4, 1, @"z:\a\a.dat");
TryDeleting(master, 4, 1, @"\a\a.dat");
TryDeleting(master, 4, 1, @"a/a.dat");
TryDeleting(master, 4, 0, 0);
TryDeleting(master, 4, 0, 1);
TryDeleting(master, 4, 0, 2);
TryDeleting(master, 4, 0, 3);
TryDeleting(master, 4, 0, 0, 1);
TryDeleting(master, 4, 0, 0, 2);
TryDeleting(master, 4, 0, 0, 3);
TryDeleting(master, 4, 0, 1, 2);
TryDeleting(master, 4, 0, 1, 3);
TryDeleting(master, 4, 0, 2);
TryDeleting(master, 4, 1, 0);
TryDeleting(master, 4, 1, 1);
TryDeleting(master, 4, 3, 2);
TryDeleting(master, 4, 4, 3);
TryDeleting(master, 4, 10, 0, 1);
TryDeleting(master, 4, 10, 0, 2);
TryDeleting(master, 4, 10, 0, 3);
TryDeleting(master, 4, 20, 1, 2);
TryDeleting(master, 4, 30, 1, 3);
TryDeleting(master, 4, 40, 2);
}
示例14: SaveChannels
private void SaveChannels(ZipFile zip, string fileName, ChannelList channels, byte[] fileContent)
{
if (fileContent == null)
return;
zip.Delete(fileName);
string tempFilePath = Path.GetTempFileName();
using (var stream = new FileStream(tempFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
this.WriteChannels(channels, fileContent, stream);
}
zip.Add(tempFilePath, fileName);
}
示例15: UpdateCommentOnlyOnDisk
public void UpdateCommentOnlyOnDisk()
{
string tempFile = GetTempFilePath();
Assert.IsNotNull(tempFile, "No permission to execute this test?");
tempFile = Path.Combine(tempFile, "SharpZipTest.Zip");
if (File.Exists(tempFile))
{
File.Delete(tempFile);
}
using (ZipFile testFile = ZipFile.Create(tempFile))
{
testFile.BeginUpdate();
testFile.Add(new StringMemoryDataSource("Aha"), "No1", CompressionMethod.Stored);
testFile.Add(new StringMemoryDataSource("And so it goes"), "No2", CompressionMethod.Stored);
testFile.Add(new StringMemoryDataSource("No3"), "No3", CompressionMethod.Stored);
testFile.CommitUpdate();
Assert.IsTrue(testFile.TestArchive(true));
}
using (ZipFile testFile = new ZipFile(tempFile))
{
Assert.IsTrue(testFile.TestArchive(true));
Assert.AreEqual("", testFile.ZipFileComment);
testFile.BeginUpdate(new DiskArchiveStorage(testFile, FileUpdateMode.Direct));
testFile.SetComment("Here is my comment");
testFile.CommitUpdate();
Assert.IsTrue(testFile.TestArchive(true));
}
using (ZipFile testFile = new ZipFile(tempFile))
{
Assert.IsTrue(testFile.TestArchive(true));
Assert.AreEqual("Here is my comment", testFile.ZipFileComment);
}
File.Delete(tempFile);
// Variant using indirect updating.
using (ZipFile testFile = ZipFile.Create(tempFile))
{
testFile.BeginUpdate();
testFile.Add(new StringMemoryDataSource("Aha"), "No1", CompressionMethod.Stored);
testFile.Add(new StringMemoryDataSource("And so it goes"), "No2", CompressionMethod.Stored);
testFile.Add(new StringMemoryDataSource("No3"), "No3", CompressionMethod.Stored);
testFile.CommitUpdate();
Assert.IsTrue(testFile.TestArchive(true));
}
using (ZipFile testFile = new ZipFile(tempFile))
{
Assert.IsTrue(testFile.TestArchive(true));
Assert.AreEqual("", testFile.ZipFileComment);
testFile.BeginUpdate();
testFile.SetComment("Here is my comment");
testFile.CommitUpdate();
Assert.IsTrue(testFile.TestArchive(true));
}
using (ZipFile testFile = new ZipFile(tempFile))
{
Assert.IsTrue(testFile.TestArchive(true));
Assert.AreEqual("Here is my comment", testFile.ZipFileComment);
}
File.Delete(tempFile);
}