Example #1
0
        public static HwpV3AdditionalBlock Create(BinaryReader reader)
        {
            HwpV3AdditionalBlock result = new HwpV3AdditionalBlock();

            result.BlockType = 1;

            result.ID     = reader.ReadDWord();
            result.Length = reader.ReadDWord();

            if (result.ID == 0u && result.Length == 0u)
            {
                return(result);
            }

            result.Content = reader.ReadBytes((int)result.Length);
            return(result);
        }
Example #2
0
        public static HwpV3Document Load(string filePath)
        {
            HwpV3Document doc = new HwpV3Document();

            using (Stream file = File.OpenRead(filePath))
                using (BinaryReader reader = new BinaryReader(file))
                {
                    doc.Signature         = HwpV3DocumentSignature.Create(reader);
                    doc.Information       = HwpV3DocumentInformation.Create(reader);
                    doc.Summary           = HwpV3DocumentSummary.Create(reader);
                    doc.InformationBlocks = HwpV3DocumentInformationBlockCollection.Create(reader, doc.Information);

                    bool         requireDecompress = ((int)doc.Information.Compressed != 0);
                    MemoryStream extractedFile     = new MemoryStream();

                    byte[] buffer = new byte[64000];
                    int    read   = 0;

                    if (requireDecompress)
                    {
                        using (DeflateStream deflateStream = new DeflateStream(file, CompressionMode.Decompress, true))
                        {
                            while ((read = deflateStream.Read(buffer, 0, buffer.Length)) > 0)
                            {
                                extractedFile.Write(buffer, 0, read);
                            }
                        }
                    }
                    else
                    {
                        while ((read = file.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            extractedFile.Write(buffer, 0, read);
                        }
                    }

                    extractedFile.Seek(0L, SeekOrigin.Begin);

                    using (extractedFile)
                        using (BinaryReader subReader = new BinaryReader(extractedFile))
                        {
                            doc.FontNames        = HwpV3FontNames.Create(subReader);
                            doc.Styles           = HwpV3DocumentStyleCollection.Create(subReader);
                            doc.Paragraph        = HwpV3Paragraph.Create(subReader);
                            doc.AdditionalBlock1 = HwpV3AdditionalBlock.Create(subReader);

                            uint         additionalBlock2Size = 0u;
                            bool         hasAdditionalBlock2  = false;
                            BinaryReader target = (requireDecompress ? reader : subReader);
                            target.BaseStream.Seek(-8, SeekOrigin.End);
                            uint id = target.ReadDWord();
                            if (id == 0u)
                            {
                                additionalBlock2Size = target.ReadDWord();
                                if (additionalBlock2Size != 0u)
                                {
                                    hasAdditionalBlock2 = true;
                                }
                            }

                            if (hasAdditionalBlock2)
                            {
                                target.BaseStream.Position = target.BaseStream.Length - additionalBlock2Size;
                                doc.AdditionalBlock2       = HwpV3AdditionalBlock.Create(reader);
                            }
                        }

                    return(doc);
                }
        }