-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
ZipPackagePart.cs
105 lines (91 loc) · 3.93 KB
/
ZipPackagePart.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.IO.Compression;
namespace System.IO.Packaging
{
/// <summary>
/// This class represents a Part within a Zip container.
/// This is a part of the Packaging Layer APIs.
/// This implementation is specific to the Zip file format.
/// </summary>
public sealed class ZipPackagePart : PackagePart
{
#region Public Methods
/// <summary>
/// Custom Implementation for the GetStream Method
/// </summary>
/// <param name="streamFileMode">Mode in which the stream should be opened</param>
/// <param name="streamFileAccess">Access with which the stream should be opened</param>
/// <returns>Stream Corresponding to this part</returns>
protected override Stream? GetStreamCore(FileMode streamFileMode, FileAccess streamFileAccess)
{
if (_zipArchiveEntry != null)
{
// Reset the stream when FileMode.Create is specified. Since ZipArchiveEntry only
// ever supports opening once when the backing archive is in Create mode, we'll avoid
// calling SetLength since the stream returned won't be seekable. You could still open
// an archive in Update mode then call part.GetStream(FileMode.Create), in which case
// we'll want this call to SetLength.
if (streamFileMode == FileMode.Create && _zipArchiveEntry.Archive.Mode != ZipArchiveMode.Create)
{
using (var tempStream = _zipStreamManager.Open(_zipArchiveEntry, streamFileMode, streamFileAccess))
{
tempStream.SetLength(0);
}
}
var stream = _zipStreamManager.Open(_zipArchiveEntry, streamFileMode, streamFileAccess);
return stream;
}
return null;
}
#endregion Public Methods
#region Internal Constructors
/// <summary>
/// Constructs a ZipPackagePart for an atomic (i.e. non-interleaved) part.
/// This is called from the ZipPackage class as a result of GetPartCore,
/// GetPartsCore or CreatePartCore methods
/// </summary>
/// <param name="zipPackage"></param>
/// <param name="zipArchive"></param>
/// <param name="zipArchiveEntry"></param>
/// <param name="zipStreamManager"></param>
/// <param name="partUri"></param>
/// <param name="compressionOption"></param>
/// <param name="contentType"></param>
internal ZipPackagePart(ZipPackage zipPackage,
ZipArchive zipArchive,
ZipArchiveEntry zipArchiveEntry,
ZipStreamManager zipStreamManager,
PackUriHelper.ValidatedPartUri partUri,
string contentType,
CompressionOption compressionOption)
: base(zipPackage, partUri, contentType, compressionOption)
{
_zipPackage = zipPackage;
_zipArchive = zipArchive;
_zipStreamManager = zipStreamManager;
_zipArchiveEntry = zipArchiveEntry;
}
#endregion Internal Constructors
#region Internal Properties
/// <summary>
/// Obtain the ZipFileInfo descriptor of an atomic part.
/// </summary>
internal ZipArchiveEntry ZipArchiveEntry
{
get
{
return _zipArchiveEntry;
}
}
#endregion Internal Properties
#region Private Variables
private readonly ZipPackage _zipPackage;
private readonly ZipArchiveEntry _zipArchiveEntry;
private readonly ZipArchive _zipArchive;
private readonly ZipStreamManager _zipStreamManager;
#endregion Private Variables
}
}