本文整理汇总了C#中ICSharpCode.SharpZipLib.Zip.ZipFile类的典型用法代码示例。如果您正苦于以下问题:C# ZipFile类的具体用法?C# ZipFile怎么用?C# ZipFile使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ZipFile类属于ICSharpCode.SharpZipLib.Zip命名空间,在下文中一共展示了ZipFile类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Convert
public void Convert(EndianBinaryWriter writer)
{
var jbtWriter = new JbtWriter(writer);
var zf = new ZipFile(jarFile);
foreach (ZipEntry ze in zf)
{
if (!ze.IsFile) continue;
if (!ze.Name.EndsWith(".class")) continue;
var type = new CompileTypeInfo();
type.Read(zf.GetInputStream(ze));
var reader = new BinaryReader(zf.GetInputStream(ze));
var buffer = new byte[ze.Size];
reader.Read(buffer, 0, (int)ze.Size);
jbtWriter.Write(type.Name, buffer);
}
jbtWriter.Flush();
}
示例2: LoadPack
public static void LoadPack(string packDir)
{
Dictionary<string, Dictionary<ushort, string>> allTextMap;
using (var z = new ZipFile(Path.Combine(packDir, "text.zip")))
{
using (var texter = new StreamReader(z.GetInputStream(z.GetEntry("text.csv")), Encoding.UTF8))
{
allTextMap = CSV.ParseCSVText(texter);
}
}
var byterList = new List<BinaryReader>();
var zipFiles = new List<ZipFile>();
foreach (var f in Directory.GetFiles(packDir, "*.zip"))
{
var name = Path.GetFileNameWithoutExtension(f);
if (name != null && !name.Equals("text"))
{
var z = new ZipFile(f);
zipFiles.Add(z);
var byter = new BinaryReader(z.GetInputStream(z.GetEntry(name)));
byterList.Add(byter);
}
}
Processor(new Stream(byterList, allTextMap));
foreach (var z in zipFiles)
{
z.Close();
}
}
示例3: ListZipFile
static void ListZipFile(string fileName, string fileFilter, string directoryFilter)
{
using (ZipFile zipFile = new ZipFile(fileName)) {
PathFilter localFileFilter = new PathFilter(fileFilter);
PathFilter localDirFilter = new PathFilter(directoryFilter);
if ( zipFile.Count == 0 ) {
Console.WriteLine("No entries to list");
}
else {
for ( int i = 0 ; i < zipFile.Count; ++i)
{
ZipEntry e = zipFile[i];
if ( e.IsFile ) {
string path = Path.GetDirectoryName(e.Name);
if ( localDirFilter.IsMatch(path) ) {
if ( localFileFilter.IsMatch(Path.GetFileName(e.Name)) ) {
Console.WriteLine(e.Name);
}
}
}
else if ( e.IsDirectory ) {
if ( localDirFilter.IsMatch(e.Name) ) {
Console.WriteLine(e.Name);
}
}
else {
Console.WriteLine(e.Name);
}
}
}
}
}
示例4: Basics
public void Basics()
{
const string tempName1 = "a(1).dat";
MemoryStream target = new MemoryStream();
string tempFilePath = GetTempFilePath();
Assert.IsNotNull(tempFilePath, "No permission to execute this test?");
string addFile = Path.Combine(tempFilePath, tempName1);
MakeTempFile(addFile, 1);
try {
FastZip fastZip = new FastZip();
fastZip.CreateZip(target, tempFilePath, false, @"a\(1\)\.dat", null);
MemoryStream archive = new MemoryStream(target.ToArray());
using (ZipFile zf = new ZipFile(archive)) {
Assert.AreEqual(1, zf.Count);
ZipEntry entry = zf[0];
Assert.AreEqual(tempName1, entry.Name);
Assert.AreEqual(1, entry.Size);
Assert.IsTrue(zf.TestArchive(true));
zf.Close();
}
}
finally {
File.Delete(tempName1);
}
}
示例5: UnZip
/// <summary>
/// 解压数据。
/// </summary>
/// <param name="path"></param>
/// <param name="handler"></param>
public static void UnZip(string path, UnZipStreamHanlder handler)
{
if (File.Exists(path) && handler != null)
{
lock (typeof(ZipTools))
{
using (ZipFile zip = new ZipFile(File.Open(path, FileMode.Open, FileAccess.Read)))
{
foreach (ZipEntry entry in zip)
{
if (entry.IsFile && !string.IsNullOrEmpty(entry.Name))
{
using (Stream stream = zip.GetInputStream(entry))
{
if (stream != null)
{
handler(entry.Name, stream);
}
}
}
}
}
}
}
}
示例6: ExtendedData
public ExtendedData(string fileName, string mimeType, ZipFile zipFile, ZipEntry extendedZipEntry)
{
FileName = fileName;
MimeType = mimeType;
_ExtendedZipEntry = extendedZipEntry;
_ZipFile = zipFile;
}
示例7: UnZip
private static bool UnZip(string file)
{
var folder = Path.GetDirectoryName(file);
if (folder == null) return false;
using (var fs = File.OpenRead(file))
{
using (var zf = new ZipFile(fs))
{
foreach (ZipEntry entry in zf)
{
if (!entry.IsFile) continue; // Handle directories below
var outputFile = Path.Combine(folder, entry.Name);
var dir = Path.GetDirectoryName(outputFile);
if (dir == null) continue;
if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);
using (var stream = zf.GetInputStream(entry))
{
using (var sw = File.Create(outputFile))
{
stream.CopyTo(sw);
}
}
}
}
}
return true;
}
示例8: 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);
}
}
}
}
示例9: GetIpaPList
private static Dictionary<string, object> GetIpaPList(string filePath)
{
var plist = new Dictionary<string, object>();
var zip = new ZipInputStream(File.OpenRead(filePath));
using (var filestream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
var zipfile = new ZipFile(filestream);
ZipEntry item;
while ((item = zip.GetNextEntry()) != null)
{
Match match = Regex.Match(item.Name.ToLower(), @"Payload/([A-Za-z0-9\-. ]+)\/info.plist$",
RegexOptions.IgnoreCase);
if (match.Success)
{
var bytes = new byte[50*1024];
using (Stream strm = zipfile.GetInputStream(item))
{
int size = strm.Read(bytes, 0, bytes.Length);
using (var s = new BinaryReader(strm))
{
var bytes2 = new byte[size];
Array.Copy(bytes, bytes2, size);
plist = (Dictionary<string, object>) PlistCS.readPlist(bytes2);
}
}
break;
}
}
}
return plist;
}
示例10: unzipDataFiles
private static void unzipDataFiles(string rootDir, string zipFile)
{
var zipFilePath = Path.Combine(rootDir, zipFile);
var unzippedDirName = zipFile.Replace(".zip", "");
var fs = System.IO.File.OpenRead(zipFilePath);
var zf = new ZipFile(fs);
foreach (ZipEntry entry in zf)
{
if (entry.IsDirectory)
{
var directoryName = Path.Combine(rootDir, entry.Name);
if (!Directory.Exists(directoryName)) Directory.CreateDirectory(directoryName);
continue;
}
var entryFileName = entry.Name;
Console.WriteLine("Unzipping {0}", entryFileName);
var buffer = new byte[4096];
var zipStream = zf.GetInputStream(entry);
var unzippedFilePath = Path.Combine(rootDir, entryFileName);
using (var fsw = System.IO.File.Create(unzippedFilePath))
{
StreamUtils.Copy(zipStream, fsw, buffer);
}
}
zf.IsStreamOwner = true;
zf.Close();
}
示例11: OpenZipFile
public void OpenZipFile()
{
ZipFile zipFile = new ZipFile(zipFileName);
Dictionary<string, TestFile> xmlFiles = new Dictionary<string, TestFile>();
// Decompress XML files
foreach(ZipEntry zipEntry in zipFile.Cast<ZipEntry>().Where(zip => zip.IsFile && zip.Name.EndsWith(".xml"))) {
Stream stream = zipFile.GetInputStream(zipEntry);
string content = new StreamReader(stream).ReadToEnd();
xmlFiles.Add(zipEntry.Name, new TestFile { Name = zipEntry.Name, Content = content });
}
// Add descriptions
foreach(TestFile metaData in xmlFiles.Values.Where(f => f.Name.StartsWith("ibm/ibm_oasis"))) {
var doc = System.Xml.Linq.XDocument.Parse(metaData.Content);
foreach(var testElem in doc.Descendants("TEST")) {
string uri = "ibm/" + testElem.Attribute("URI").Value;
string description = testElem.Value.Replace("\n ", "\n").TrimStart('\n');
if (xmlFiles.ContainsKey(uri))
xmlFiles[uri].Description = description;
}
}
// Copy canonical forms
foreach(TestFile canonical in xmlFiles.Values.Where(f => f.Name.Contains("/out/"))) {
string uri = canonical.Name.Replace("/out/", "/");
if (xmlFiles.ContainsKey(uri))
xmlFiles[uri].Canonical = canonical.Content;
}
// Copy resuts to field
this.xmlFiles.AddRange(xmlFiles.Values.Where(f => !f.Name.Contains("/out/")));
}
示例12: GetInternalAvc
public AvcVersion GetInternalAvc(CkanModule module, string filePath, string internalFilePath = null)
{
using (var zipfile = new ZipFile(filePath))
{
return GetInternalAvc(module, zipfile, internalFilePath);
}
}
示例13: EPubFile
public EPubFile(Stream s)
{
try
{
zip = new ZipFile(s);
string mpath = getRootFromContainer
(GetContent("META-INF/container.xml"));
OPFParser.ParseStream(GetContent(mpath), mpath,
out Manifest, out Spine);
if (Spine.TocId == null)
{
Toc = new TableOfContents();
}
else
{
string tocPath = Manifest.GetById(Spine.TocId).Linkref;
Toc = NCXParser.ParseStream(GetContent(tocPath), tocPath);
}
stream = s;
}
catch (Exception ex)
{
s.Dispose();
throw ex;
}
}
示例14: AddToCbz
public static bool AddToCbz(string fileName, string zipFileName)
{
if (Path.GetExtension (zipFileName).Length == 0) {
zipFileName = Path.ChangeExtension (zipFileName, ".zip");
}
ZipFile zipFile;
try {
if (File.Exists (zipFileName)) {
zipFile = new ZipFile (zipFileName);
} else {
zipFile = ZipFile.Create (zipFileName);
}
using (zipFile) {
zipFile.UseZip64 = UseZip64.Off;
zipFile.BeginUpdate ();
zipFile.Add (Path.GetFullPath (fileName));
zipFile.CommitUpdate ();
return true;
}
} catch {
return false;
}
}
示例15: ExtractZipFile
public static void ExtractZipFile(string archiveFilenameIn, string outFolder,
Func<ZipEntry, String, bool> entryCheckFunc,
string password = null)
{
ZipFile zf = null;
try
{
FileStream fs = File.OpenRead(archiveFilenameIn);
zf = new ZipFile(fs);
if (!String.IsNullOrEmpty(password))
{
zf.Password = password; // AES encrypted entries are handled automatically
}
foreach (ZipEntry zipEntry in zf)
{
if (!zipEntry.IsFile)
{
continue; // Ignore directories
}
String entryFileName = zipEntry.Name;
// to remove the folder from the entry:- entryFileName = Path.GetFileName(entryFileName);
// Optionally match entrynames against a selection list here to skip as desired.
// The unpacked length is available in the zipEntry.Size property.
// Manipulate the output filename here as desired.
String fullZipToPath = Path.Combine(outFolder, entryFileName);
string directoryName = Path.GetDirectoryName(fullZipToPath);
if (entryCheckFunc(zipEntry, fullZipToPath)) continue;
if (directoryName.Length > 0)
Directory.CreateDirectory(directoryName);
byte[] buffer = new byte[4096]; // 4K is optimum
Stream zipStream = zf.GetInputStream(zipEntry);
// Unzip file in buffered chunks. This is just as fast as unpacking to a buffer the full size
// of the file, but does not waste memory.
// The "using" will close the stream even if an exception occurs.
using (FileStream streamWriter = File.Create(fullZipToPath))
{
StreamUtils.Copy(zipStream, streamWriter, buffer);
}
}
}
catch (Exception ex)
{
throw new Exception("ExtractZipFile failed", ex);
}
finally
{
if (zf != null)
{
zf.IsStreamOwner = true; // Makes close also shut the underlying stream
zf.Close(); // Ensure we release resources
}
}
}