Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.ExtLibs/HeuristicLab.EPPlus/4.0.3/EPPlus-4.0.3/Packaging/ZipPackagePart.cs @ 18240

Last change on this file since 18240 was 12074, checked in by sraggl, 10 years ago

#2341: Added EPPlus-4.0.3 to ExtLibs

File size: 6.2 KB
Line 
1/*******************************************************************************
2 * You may amend and distribute as you like, but don't remove this header!
3 *
4 * EPPlus provides server-side generation of Excel 2007/2010 spreadsheets.
5 * See http://www.codeplex.com/EPPlus for details.
6 *
7 * Copyright (C) 2011  Jan Källman
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
17 * See the GNU Lesser General Public License for more details.
18 *
19 * The GNU Lesser General Public License can be viewed at http://www.opensource.org/licenses/lgpl-license.php
20 * If you unfamiliar with this license or have questions about it, here is an http://www.gnu.org/licenses/gpl-faq.html
21 *
22 * All code and executables are provided "as is" with no warranty either express or implied.
23 * The author accepts no liability for any damage or loss of business that this product may cause.
24 *
25 * Code change notes:
26 *
27 * Author             Change            Date
28 *******************************************************************************
29 * Jan Källman    Added   25-Oct-2012
30 *******************************************************************************/
31using System;
32using System.Collections.Generic;
33using System.Linq;
34using System.Text;
35using System.IO;
36using OfficeOpenXml.Packaging.Ionic.Zip;
37
38namespace OfficeOpenXml.Packaging
39{
40    internal class ZipPackagePart : ZipPackageRelationshipBase, IDisposable
41    {
42        internal delegate void SaveHandlerDelegate(ZipOutputStream stream, CompressionLevel compressionLevel, string fileName);
43
44        internal ZipPackagePart(ZipPackage package, ZipEntry entry)
45        {
46            Package = package;
47            Entry = entry;
48            SaveHandler = null;
49            Uri = new Uri(package.GetUriKey(entry.FileName), UriKind.Relative);
50        }
51        internal ZipPackagePart(ZipPackage package, Uri partUri, string contentType, CompressionLevel compressionLevel)
52        {
53            Package = package;
54            //Entry = new ZipEntry();
55            //Entry.FileName = partUri.OriginalString.Replace('/','\\');
56            Uri = partUri;
57            ContentType = contentType;
58            CompressionLevel = compressionLevel;
59        }
60        internal ZipPackage Package { get; set; }
61        internal ZipEntry Entry { get; set; }
62        internal CompressionLevel CompressionLevel;
63        MemoryStream _stream = null;
64        internal MemoryStream Stream
65        {
66            get
67            {
68                return _stream;
69            }
70            set
71            {
72                _stream = value;
73            }
74        }
75        internal override ZipPackageRelationship CreateRelationship(Uri targetUri, TargetMode targetMode, string relationshipType)
76        {
77
78            var rel = base.CreateRelationship(targetUri, targetMode, relationshipType);
79            rel.SourceUri = Uri;
80            return rel;
81        }
82        internal MemoryStream GetStream()
83        {
84            return GetStream(FileMode.OpenOrCreate, FileAccess.ReadWrite);
85        }
86        internal MemoryStream GetStream(FileMode fileMode)
87        {
88            return GetStream(FileMode.Create, FileAccess.ReadWrite);
89        }
90        internal MemoryStream GetStream(FileMode fileMode, FileAccess fileAccess)
91        {
92            if (_stream == null || fileMode == FileMode.CreateNew || fileMode == FileMode.Create)
93            {
94                _stream = new MemoryStream();
95            }
96            else
97            {
98                _stream.Seek(0, SeekOrigin.Begin);               
99            }
100            return _stream;
101        }
102
103        string _contentType = "";
104        public string ContentType
105        {
106            get
107            {
108                return _contentType;
109            }
110            internal set
111            {
112                if (!string.IsNullOrEmpty(_contentType))
113                {
114                    if (Package._contentTypes.ContainsKey(Package.GetUriKey(Uri.OriginalString)))
115                    {
116                        Package._contentTypes.Remove(Package.GetUriKey(Uri.OriginalString));
117                        Package._contentTypes.Add(Package.GetUriKey(Uri.OriginalString), new ZipPackage.ContentType(value, false, Uri.OriginalString));
118                    }
119                }
120                _contentType = value;
121            }
122        }
123        public Uri Uri { get; private set; }
124        public Stream GetZipStream()
125        {
126            MemoryStream ms = new MemoryStream();
127            ZipOutputStream os = new ZipOutputStream(ms);
128            return os;
129        }
130        internal SaveHandlerDelegate SaveHandler
131        {
132            get;
133            set;
134        }
135        internal void WriteZip(ZipOutputStream os)
136        {
137            byte[] b;
138            if (SaveHandler == null)
139            {
140                b = GetStream().ToArray();
141                if (b.Length == 0)   //Make sure the file isn't empty. DotNetZip streams does not seems to handle zero sized files.
142                {
143                    return;
144                }
145                os.CompressionLevel = (OfficeOpenXml.Packaging.Ionic.Zlib.CompressionLevel)CompressionLevel;
146                os.PutNextEntry(Uri.OriginalString);
147                os.Write(b, 0, b.Length);
148            }
149            else
150            {
151                SaveHandler(os, (CompressionLevel)CompressionLevel, Uri.OriginalString);
152            }
153
154            if (_rels.Count > 0)
155            {
156                string f = Uri.OriginalString;
157                var fi = new FileInfo(f);
158                _rels.WriteZip(os, (string.Format("{0}_rels/{1}.rels", f.Substring(0, f.Length - fi.Name.Length), fi.Name)));
159            }
160            b = null;
161        }
162
163
164        public void Dispose()
165        {
166            _stream.Close();
167            _stream.Dispose();
168        }
169    }
170}
Note: See TracBrowser for help on using the repository browser.