Exemple #1
0
        /// <summary>
        /// Extracts the zip.
        /// </summary>
        /// <param name="zipStream">zip stream</param>
        /// <returns>Instance of MemoryFolder</returns>
        public static MemoryFolder ExtractZip(Stream zipStream)
        {
            if ((zipStream == null) || !zipStream.CanRead)
            {
                return(null);
            }
            MemoryFolder folder  = new MemoryFolder();
            ZipArchive   archive = new ZipArchive(zipStream);

            foreach (ZipArchiveEntry entry in archive.Entries)
            {
                string fullName = entry.FullName;
                using (Stream stream = entry.Open())
                {
                    MemoryStream stream2 = new MemoryStream();
                    int          count   = 0x800;
                    byte[]       buffer  = new byte[count];
                    while (true)
                    {
                        count = stream.Read(buffer, 0, buffer.Length);
                        if (count <= 0)
                        {
                            break;
                        }
                        stream2.Write(buffer, 0, count);
                    }
                    stream2.Seek(0L, (SeekOrigin)SeekOrigin.Begin);
                    folder.CreateMemoryFile(fullName, (Stream)stream2);
                }
            }
            return(folder);
        }
Exemple #2
0
 public void LoadPackageRelationFiles(MemoryFolder mFolder)
 {
     if (mFolder != null)
     {
         List <Dt.Xls.OOXml.Relationship> relationshipsByBaseName = GetRelationshipsByBaseName(this.FileName, mFolder);
         if (relationshipsByBaseName != null)
         {
             foreach (Dt.Xls.OOXml.Relationship relationship in relationshipsByBaseName)
             {
                 string target = relationship.Target;
                 if ((target.IndexOf('/') != 0) && !IsAbsolutePath(target))
                 {
                     target = (((this.FileName == null) || (this.FileName.Length == 0)) ? "" : (Path.GetDirectoryName(this.FileName) + @"\")) + target;
                 }
                 target = FixFileName(target);
                 XFile file = new XFile(target, relationship.Type)
                 {
                     Relationship = relationship
                 };
                 if (!IsAbsolutePath(target))
                 {
                     file.LoadPackageRelationFiles(mFolder);
                 }
                 if (this.RelationFiles.ContainsKey(relationship.Id))
                 {
                     this.RelationFiles[relationship.Id] = file;
                 }
                 else
                 {
                     this.RelationFiles.Add(relationship.Id, file);
                 }
             }
         }
     }
 }
Exemple #3
0
        /// <summary>
        /// Compresses the files.
        /// </summary>
        /// <param name="mFolder">m folder</param>
        /// <param name="targetStream">target stream</param>
        /// <returns>successful or not</returns>
        public static bool CompressFiles(MemoryFolder mFolder, Stream targetStream)
        {
            if (((mFolder == null) || (mFolder.disk == null)) || ((targetStream == null) || !targetStream.CanWrite))
            {
                return(false);
            }
            ZipArchive zipOutputStream = GetZipOutputStream(targetStream);

            if (zipOutputStream == null)
            {
                return(false);
            }
            using (zipOutputStream)
            {
                foreach (string str in mFolder.disk.Keys)
                {
                    Stream file = mFolder.GetFile(str);
                    if (file != null)
                    {
                        CompressFile(zipOutputStream, str.Replace('\\', '/'), file);
                    }
                    else
                    {
                        zipOutputStream.CreateEntry(str.Replace('\\', '/'));
                    }
                }
            }
            return(true);
        }
Exemple #4
0
        public static void SaveContentTypes(XFile file, MemoryFolder mFolder, ExcelFileType workbookType)
        {
            CT_Types types = GetContentTypes(file, mFolder, workbookType);

            if (types != null)
            {
                mFolder.CreateMemoryFile("[Content_Types].xml", CreateStreamFromObject(types, typeof(CT_Types)));
            }
        }
Exemple #5
0
        private static bool UpdateFileList(List <object> types, XFile xFile, MemoryFolder mFolder, ExcelFileType workbookType)
        {
            if (((types == null) || (xFile == null)) || (mFolder == null))
            {
                return(false);
            }
            bool flag = true;

            if (!string.IsNullOrEmpty(xFile.FileName))
            {
                string str = ConvertRelationshipType2ContentType(xFile.FileType, workbookType);
                if (!string.IsNullOrEmpty(str))
                {
                    CT_Override @override = new CT_Override {
                        PartName    = xFile.FileName.Replace('\\', '/'),
                        ContentType = str
                    };
                    if (new List <string> {
                        "application/vnd.openxmlformats-officedocument.spreadsheetml.comments+xml", "application/vnd.openxmlformats-officedocument.drawingml.diagramColors+xml", "application/vnd.openxmlformats-officedocument.drawingml.diagramData+xml", "application/vnd.ms-office.drawingml.diagramDrawing+xml", "application/vnd.openxmlformats-officedocument.drawingml.diagramLayout+xml", "application/vnd.openxmlformats-officedocument.drawingml.diagramStyle+xml", "application/vnd.ms-excel.controlproperties+xml", "application/vnd.openxmlformats-officedocument.vmlDrawing"
                    }.Contains(@override.ContentType))
                    {
                        @override.PartName = xFile.Target.Replace("..", "/xl");
                    }
                    bool flag2 = false;
                    foreach (object obj2 in types)
                    {
                        if (obj2 is CT_Override)
                        {
                            CT_Override override2 = obj2 as CT_Override;
                            if ((override2.ContentType == @override.ContentType) && (override2.PartName == @override.PartName))
                            {
                                flag2 = true;
                                break;
                            }
                        }
                    }
                    if (!flag2)
                    {
                        types.Add(@override);
                    }
                }
            }
            if ((xFile.RelationFiles != null) && (xFile.RelationFiles.Count > 0))
            {
                foreach (XFile file in xFile.RelationFiles.Values)
                {
                    if (file != null)
                    {
                        flag = flag && UpdateFileList(types, file, mFolder, workbookType);
                    }
                }
            }
            return(flag);
        }
Exemple #6
0
        /// <summary>
        /// Clones this instance.
        /// </summary>
        /// <returns>Returns the cloned memory folder.</returns>
        public MemoryFolder Clone()
        {
            MemoryFolder folder = new MemoryFolder {
                disk = new Dictionary <string, Stream>()
            };

            foreach (string str in this.disk.Keys)
            {
                folder.disk.Add(str, this.disk[str]);
            }
            folder._currentPath = this._currentPath;
            return(folder);
        }
Exemple #7
0
        public static CT_Types GetContentTypes(XFile rootFile, MemoryFolder mFolder, ExcelFileType workbookType)
        {
            if (((rootFile == null) || (rootFile.RelationFiles == null)) || ((rootFile.RelationFiles.Count == 0) || (mFolder == null)))
            {
                return(null);
            }
            List <object> defaultType = GetDefaultType();

            if (defaultType == null)
            {
                return(null);
            }
            if (!UpdateFileList(defaultType, rootFile, mFolder, workbookType))
            {
                return(null);
            }
            return(new CT_Types {
                Items = defaultType.ToArray()
            });
        }
Exemple #8
0
 private static void SavePackageRelationFiles(XFile xFile, MemoryFolder mFolder)
 {
     if (((xFile != null) && (xFile.RelationFiles != null)) && ((xFile.RelationFiles.Count != 0) && (mFolder != null)))
     {
         string str  = string.IsNullOrEmpty(xFile.FileName) ? "" : Path.GetDirectoryName(xFile.FileName);
         string str2 = string.IsNullOrEmpty(xFile.FileName) ? "" : Path.GetFileName(xFile.FileName);
         Dictionary <string, string[]> htRelationShip = new Dictionary <string, string[]>();
         foreach (string str3 in xFile.RelationFiles.Keys)
         {
             XFile file = xFile.RelationFiles[str3];
             if (file != null)
             {
                 string[] strArray = new string[2];
                 if (!string.IsNullOrWhiteSpace(file.Target))
                 {
                     strArray[0] = file.Target;
                 }
                 else
                 {
                     strArray[0] = file.FileName.Replace('\\', '/');
                 }
                 strArray[1] = file.FileType;
                 if (htRelationShip.ContainsKey(str3))
                 {
                     htRelationShip[str3] = strArray;
                 }
                 else
                 {
                     htRelationShip.Add(str3, strArray);
                 }
             }
             SavePackageRelationFiles(file, mFolder);
         }
         CT_Relationships relationships = ToRelationships(htRelationShip);
         if (relationships != null)
         {
             mFolder.CreateMemoryFile(str + @"\_rels\" + str2 + ".rels", PackageXml.CreateStreamFromObject(relationships, typeof(CT_Relationships)));
         }
     }
 }
Exemple #9
0
 public void SavePackageRelationFiles(MemoryFolder mFolder)
 {
     SavePackageRelationFiles(this, mFolder);
 }
Exemple #10
0
        public static List <Dt.Xls.OOXml.Relationship> GetRelationshipsByBaseName(string baseName, MemoryFolder mFolder)
        {
            if ((baseName == null) || (mFolder == null))
            {
                return(null);
            }
            string relationshipsNameByBaseName = GetRelationshipsNameByBaseName(baseName);

            if (relationshipsNameByBaseName == null)
            {
                return(null);
            }
            return(ReadRelationships(mFolder.GetFile(relationshipsNameByBaseName)));
        }
Exemple #11
0
        public static void WriteWorkbookFile(XFile workbookFile, List <SheetInfo> sheets, MemoryFolder mFolder)
        {
            MemoryStream stream = new MemoryStream();

            new XmlWriterSettings().Encoding = Encoding.UTF8;
            XmlWriter writer = XmlWriter.Create((Stream)stream, new XmlWriterSettings());

            writer.WriteStartDocument();
            writer.WriteStartElement("workbook", "http://schemas.openxmlformats.org/spreadsheetml/2006/main");
            writer.WriteAttributeString("xmlns", "r", null, "http://schemas.openxmlformats.org/officeDocument/2006/relationships");
            writer.WriteStartElement("sheets");
            foreach (SheetInfo info in sheets)
            {
                writer.WriteStartElement("sheet");
                writer.WriteAttributeString("name", info.name);
                writer.WriteAttributeString("sheetId", ((uint)info.sheetID).ToString());
                writer.WriteAttributeString("r", "id", "http://schemas.openxmlformats.org/officeDocument/2006/relationships", info.rID);
                writer.WriteEndElement();
            }
            writer.WriteEndElement();
            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Flush();
            stream.Seek(0L, (SeekOrigin)SeekOrigin.Begin);
            mFolder.CreateMemoryFile(workbookFile.FileName, (Stream)stream);
        }