本文整理汇总了C#中ZipArchive.GetEntry方法的典型用法代码示例。如果您正苦于以下问题:C# ZipArchive.GetEntry方法的具体用法?C# ZipArchive.GetEntry怎么用?C# ZipArchive.GetEntry使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ZipArchive
的用法示例。
在下文中一共展示了ZipArchive.GetEntry方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateModeInvalidOperations
public static void CreateModeInvalidOperations()
{
MemoryStream ms = new MemoryStream();
ZipArchive z = new ZipArchive(ms, ZipArchiveMode.Create);
Assert.Throws<NotSupportedException>(() => { var x = z.Entries; }); //"Entries not applicable on Create"
Assert.Throws<NotSupportedException>(() => z.GetEntry("dirka")); //"GetEntry not applicable on Create"
ZipArchiveEntry e = z.CreateEntry("hey");
Assert.Throws<NotSupportedException>(() => e.Delete()); //"Can't delete new entry"
Stream s = e.Open();
Assert.Throws<NotSupportedException>(() => s.ReadByte()); //"Can't read on new entry"
Assert.Throws<NotSupportedException>(() => s.Seek(0, SeekOrigin.Begin)); //"Can't seek on new entry"
Assert.Throws<NotSupportedException>(() => s.Position = 0); //"Can't set position on new entry"
Assert.Throws<NotSupportedException>(() => { var x = s.Length; }); //"Can't get length on new entry"
Assert.Throws<IOException>(() => e.LastWriteTime = new DateTimeOffset()); //"Can't get LastWriteTime on new entry"
Assert.Throws<InvalidOperationException>(() => { var x = e.Length; }); //"Can't get length on new entry"
Assert.Throws<InvalidOperationException>(() => { var x = e.CompressedLength; }); //"can't get CompressedLength on new entry"
Assert.Throws<IOException>(() => z.CreateEntry("bad"));
s.Dispose();
Assert.Throws<ObjectDisposedException>(() => s.WriteByte(25)); //"Can't write to disposed entry"
Assert.Throws<IOException>(() => e.Open());
Assert.Throws<IOException>(() => e.LastWriteTime = new DateTimeOffset());
Assert.Throws<InvalidOperationException>(() => { var x = e.Length; });
Assert.Throws<InvalidOperationException>(() => { var x = e.CompressedLength; });
ZipArchiveEntry e1 = z.CreateEntry("e1");
ZipArchiveEntry e2 = z.CreateEntry("e2");
Assert.Throws<IOException>(() => e1.Open()); //"Can't open previous entry after new entry created"
z.Dispose();
Assert.Throws<ObjectDisposedException>(() => z.CreateEntry("dirka")); //"Can't create after dispose"
}
示例2: OverwriteEntry
public static async Task OverwriteEntry()
{
//Overwrite file
Stream testArchive = await StreamHelpers.CreateTempCopyStream(zfile("normal.zip"));
using (ZipArchive archive = new ZipArchive(testArchive, ZipArchiveMode.Update, true))
{
string fileName = zmodified(Path.Combine("overwrite", "first.txt"));
ZipArchiveEntry e = archive.GetEntry("first.txt");
var file = FileData.GetFile(fileName);
e.LastWriteTime = file.LastModifiedDate;
using (var stream = await StreamHelpers.CreateTempCopyStream(fileName))
{
using (Stream es = e.Open())
{
es.SetLength(0);
stream.CopyTo(es);
}
}
}
IsZipSameAsDir(testArchive, zmodified("overwrite"), ZipArchiveMode.Read, requireExplicit: true, checkTimes: true);
}
示例3: UpdateModeInvalidOperations
public static async Task UpdateModeInvalidOperations()
{
using (LocalMemoryStream ms = await LocalMemoryStream.readAppFileAsync(zfile("normal.zip")))
{
ZipArchive target = new ZipArchive(ms, ZipArchiveMode.Update, true);
ZipArchiveEntry edeleted = target.GetEntry("first.txt");
Stream s = edeleted.Open();
//invalid ops while entry open
Assert.Throws<IOException>(() => edeleted.Open());
Assert.Throws<InvalidOperationException>(() => { var x = edeleted.Length; });
Assert.Throws<InvalidOperationException>(() => { var x = edeleted.CompressedLength; });
Assert.Throws<IOException>(() => edeleted.Delete());
s.Dispose();
//invalid ops on stream after entry closed
Assert.Throws<ObjectDisposedException>(() => s.ReadByte());
Assert.Throws<InvalidOperationException>(() => { var x = edeleted.Length; });
Assert.Throws<InvalidOperationException>(() => { var x = edeleted.CompressedLength; });
edeleted.Delete();
//invalid ops while entry deleted
Assert.Throws<InvalidOperationException>(() => edeleted.Open());
Assert.Throws<InvalidOperationException>(() => { edeleted.LastWriteTime = new DateTimeOffset(); });
ZipArchiveEntry e = target.GetEntry("notempty/second.txt");
target.Dispose();
Assert.Throws<ObjectDisposedException>(() => { var x = target.Entries; });
Assert.Throws<ObjectDisposedException>(() => target.CreateEntry("dirka"));
Assert.Throws<ObjectDisposedException>(() => e.Open());
Assert.Throws<ObjectDisposedException>(() => e.Delete());
Assert.Throws<ObjectDisposedException>(() => { e.LastWriteTime = new DateTimeOffset(); });
}
}
示例4: AppendToEntry
public static async Task AppendToEntry()
{
//append
Stream testArchive = await StreamHelpers.CreateTempCopyStream(zfile("normal.zip"));
using (ZipArchive archive = new ZipArchive(testArchive, ZipArchiveMode.Update, true))
{
ZipArchiveEntry e = archive.GetEntry("first.txt");
using (StreamWriter s = new StreamWriter(e.Open()))
{
s.BaseStream.Seek(0, SeekOrigin.End);
s.Write("\r\n\r\nThe answer my friend, is blowin' in the wind.");
}
var file = FileData.GetFile(zmodified(Path.Combine("append", "first.txt")));
e.LastWriteTime = file.LastModifiedDate;
}
IsZipSameAsDir(testArchive, zmodified("append"), ZipArchiveMode.Read, requireExplicit: true, checkTimes: true);
}
示例5: DeleteAndMoveEntries
public static async Task DeleteAndMoveEntries()
{
//delete and move
var testArchive = await StreamHelpers.CreateTempCopyStream(zfile("normal.zip"));
using (ZipArchive archive = new ZipArchive(testArchive, ZipArchiveMode.Update, true))
{
ZipArchiveEntry toBeDeleted = archive.GetEntry("binary.wmv");
toBeDeleted.Delete();
toBeDeleted.Delete(); //delete twice should be okay
ZipArchiveEntry moved = archive.CreateEntry("notempty/secondnewname.txt");
ZipArchiveEntry orig = archive.GetEntry("notempty/second.txt");
using (Stream origMoved = orig.Open(), movedStream = moved.Open())
{
origMoved.CopyTo(movedStream);
}
moved.LastWriteTime = orig.LastWriteTime;
orig.Delete();
}
IsZipSameAsDir(testArchive, zmodified("deleteMove"), ZipArchiveMode.Read, requireExplicit: true, checkTimes: true);
}
示例6: ReadInterleaved
public static async Task ReadInterleaved()
{
using (ZipArchive archive = new ZipArchive(await StreamHelpers.CreateTempCopyStream(ZipTest.zfile("normal.zip"))))
{
ZipArchiveEntry e1 = archive.GetEntry("first.txt");
ZipArchiveEntry e2 = archive.GetEntry("notempty/second.txt");
//read all of e1 and e2's contents
Byte[] e1readnormal = new Byte[e1.Length];
Byte[] e2readnormal = new Byte[e2.Length];
Byte[] e1interleaved = new Byte[e1.Length];
Byte[] e2interleaved = new Byte[e2.Length];
using (Stream e1s = e1.Open())
{
ZipTest.ReadBytes(e1s, e1readnormal, e1.Length);
}
using (Stream e2s = e2.Open())
{
ZipTest.ReadBytes(e2s, e2readnormal, e2.Length);
}
//now read interleaved, assume we are working with < 4gb files
const Int32 bytesAtATime = 15;
using (Stream e1s = e1.Open(), e2s = e2.Open())
{
Int32 e1pos = 0;
Int32 e2pos = 0;
while (e1pos < e1.Length || e2pos < e2.Length)
{
if (e1pos < e1.Length)
{
Int32 e1bytesRead = e1s.Read(e1interleaved, e1pos,
bytesAtATime + e1pos > e1.Length ? (Int32)e1.Length - e1pos : bytesAtATime);
e1pos += e1bytesRead;
}
if (e2pos < e2.Length)
{
Int32 e2bytesRead = e2s.Read(e2interleaved, e2pos,
bytesAtATime + e2pos > e2.Length ? (Int32)e2.Length - e2pos : bytesAtATime);
e2pos += e2bytesRead;
}
}
}
//now compare to original read
ZipTest.ArraysEqual<Byte>(e1readnormal, e1interleaved, e1readnormal.Length);
ZipTest.ArraysEqual<Byte>(e2readnormal, e2interleaved, e2readnormal.Length);
//now read one entry interleaved
Byte[] e1selfInterleaved1 = new Byte[e1.Length];
Byte[] e1selfInterleaved2 = new Byte[e2.Length];
using (Stream s1 = e1.Open(), s2 = e1.Open())
{
Int32 s1pos = 0;
Int32 s2pos = 0;
while (s1pos < e1.Length || s2pos < e1.Length)
{
if (s1pos < e1.Length)
{
Int32 s1bytesRead = s1.Read(e1interleaved, s1pos,
bytesAtATime + s1pos > e1.Length ? (Int32)e1.Length - s1pos : bytesAtATime);
s1pos += s1bytesRead;
}
if (s2pos < e1.Length)
{
Int32 s2bytesRead = s2.Read(e2interleaved, s2pos,
bytesAtATime + s2pos > e1.Length ? (Int32)e1.Length - s2pos : bytesAtATime);
s2pos += s2bytesRead;
}
}
}
//now compare to original read
ZipTest.ArraysEqual<Byte>(e1readnormal, e1selfInterleaved1, e1readnormal.Length);
ZipTest.ArraysEqual<Byte>(e1readnormal, e1selfInterleaved2, e1readnormal.Length);
}
}
示例7: ReadModeInvalidOpsTest
public static async Task ReadModeInvalidOpsTest()
{
ZipArchive archive = new ZipArchive(await StreamHelpers.CreateTempCopyStream(ZipTest.zfile("normal.zip")), ZipArchiveMode.Read);
ZipArchiveEntry e = archive.GetEntry("first.txt");
//should also do it on deflated stream
//on archive
Assert.Throws<NotSupportedException>(() => archive.CreateEntry("hi there")); //"Should not be able to create entry"
//on entry
Assert.Throws<NotSupportedException>(() => e.Delete()); //"Should not be able to delete entry"
//Throws<NotSupportedException>(() => e.MoveTo("dirka"));
Assert.Throws<NotSupportedException>(() => e.LastWriteTime = new DateTimeOffset()); //"Should not be able to update time"
//on stream
Stream s = e.Open();
Assert.Throws<NotSupportedException>(() => s.Flush()); //"Should not be able to flush on read stream"
Assert.Throws<NotSupportedException>(() => s.WriteByte(25)); //"should not be able to write to read stream"
Assert.Throws<NotSupportedException>(() => s.Position = 4); //"should not be able to seek on read stream"
Assert.Throws<NotSupportedException>(() => s.Seek(0, SeekOrigin.Begin)); //"should not be able to seek on read stream"
Assert.Throws<NotSupportedException>(() => s.SetLength(0)); //"should not be able to resize read stream"
archive.Dispose();
//after disposed
Assert.Throws<ObjectDisposedException>(() => { var x = archive.Entries; }); //"Should not be able to get entries on disposed archive"
Assert.Throws<NotSupportedException>(() => archive.CreateEntry("dirka")); //"should not be able to create on disposed archive"
Assert.Throws<ObjectDisposedException>(() => e.Open()); //"should not be able to open on disposed archive"
Assert.Throws<NotSupportedException>(() => e.Delete()); //"should not be able to delete on disposed archive"
Assert.Throws<ObjectDisposedException>(() => { e.LastWriteTime = new DateTimeOffset(); }); //"Should not be able to update on disposed archive"
Assert.Throws<NotSupportedException>(() => s.ReadByte()); //"should not be able to read on disposed archive"
s.Dispose();
}
示例8: UpdateModifications
public static async Task UpdateModifications()
{
//delete and move
var testArchive = await StreamHelpers.CreateTempCopyStream(ZipTest.zfile("normal.zip"));
using (ZipArchive archive = new ZipArchive(testArchive, ZipArchiveMode.Update, true))
{
ZipArchiveEntry toBeDeleted = archive.GetEntry("binary.wmv");
toBeDeleted.Delete();
toBeDeleted.Delete(); //delete twice should be okay
ZipArchiveEntry moved = archive.CreateEntry("notempty/secondnewname.txt");
ZipArchiveEntry orig = archive.GetEntry("notempty/second.txt");
using (Stream origMoved = orig.Open(), movedStream = moved.Open())
{
origMoved.CopyTo(movedStream);
}
moved.LastWriteTime = orig.LastWriteTime;
orig.Delete();
}
ZipTest.IsZipSameAsDir(testArchive, ZipTest.zmodified("deleteMove"), ZipArchiveMode.Read, false, false);
//append
testArchive = await StreamHelpers.CreateTempCopyStream(ZipTest.zfile("normal.zip"));
using (ZipArchive archive = new ZipArchive(testArchive, ZipArchiveMode.Update, true))
{
ZipArchiveEntry e = archive.GetEntry("first.txt");
using (StreamWriter s = new StreamWriter(e.Open()))
{
s.BaseStream.Seek(0, SeekOrigin.End);
s.Write("\r\n\r\nThe answer my friend, is blowin' in the wind.");
}
e.LastWriteTime = new DateTimeOffset(2010, 7, 7, 11, 57, 18, new TimeSpan(-7, 0, 0));
}
ZipTest.IsZipSameAsDir(testArchive, ZipTest.zmodified("append"), ZipArchiveMode.Read, false, false);
//Overwrite file
testArchive = await StreamHelpers.CreateTempCopyStream(ZipTest.zfile("normal.zip"));
using (ZipArchive archive = new ZipArchive(testArchive, ZipArchiveMode.Update, true))
{
String fileName = ZipTest.zmodified(Path.Combine("overwrite", "first.txt"));
ZipArchiveEntry e = archive.GetEntry("first.txt");
var file = FileData.GetFile(fileName);
e.LastWriteTime = file.LastModifiedDate;
using (var stream = await StreamHelpers.CreateTempCopyStream(fileName))
{
using (Stream es = e.Open())
{
es.SetLength(0);
stream.CopyTo(es);
}
}
}
ZipTest.IsZipSameAsDir(testArchive, ZipTest.zmodified("overwrite"), ZipArchiveMode.Read, false, false);
}