예제 #1
0
        public WorkbookStream(string filePath)
        {
            using (var fs = new FileStream(filePath, FileMode.Open))
            {
                StructuredStorageReader ssr = new StructuredStorageReader(fs);
                try
                {
                    var    wbStream = ssr.GetStream("Workbook");
                    byte[] wbBytes  = new byte[wbStream.Length];
                    wbStream.Read(wbBytes, 0, wbBytes.Length, 0);
                    _biffRecords = RecordHelper.ParseBiffStreamBytes(wbBytes);
                }
                catch (StreamNotFoundException)
                {
                    var wbStream = ssr.GetStream("Book");
                    Console.WriteLine("WARNING: Main stream is in a Book record indicating legacy Excel 5 BIFF format. This may not parse correctly.");

                    byte[] wbBytes = new byte[wbStream.Length];
                    wbStream.Read(wbBytes, 0, wbBytes.Length, 0);
                    try
                    {
                        _biffRecords = RecordHelper.ParseBiffStreamBytes(wbBytes);
                    }
                    catch (Exception)
                    {
                        throw new NotImplementedException("Error parsing Book stream: Macrome currently doesn't support the Excel 5 BIFF format.");
                    }
                }
            }
        }
예제 #2
0
        /// <summary>
        /// return MainDocumentPart from doc
        /// </summary>
        /// <param name="docData"></param>
        /// <returns></returns>
        public static XmlDocument GetXMLContent(byte[] docData)
        {
            using (Stream stream = new MemoryStream(docData))
            {
                using (StructuredStorageReader reader = new StructuredStorageReader(stream))
                {
                    //parse the input document
                    WordDocument doc = new WordDocument(reader);

                    //prepare the output document
                    OpenXmlPackage.DocumentType outType = Converter.DetectOutputType(doc);
                    //string conformOutputFile = Converter.GetConformFilename(ChoosenOutputFile, outType);
                    WordprocessingDocument docx = WordprocessingDocument.Create(outType);

                    //start time
                    //DateTime start = DateTime.Now;
                    //TraceLogger.Info("Converting file {0} into {1}", InputFile, conformOutputFile);

                    //convert the document
                    return(Converter.Convert(doc, docx));

                    //DateTime end = DateTime.Now;
                    //TimeSpan diff = end.Subtract(start);
                    //TraceLogger.Info("Conversion of file {0} finished in {1} seconds", InputFile, diff.TotalSeconds.ToString(CultureInfo.InvariantCulture));
                }
            }
        }
예제 #3
0
 public void ParseabilityTest()
 {
     foreach (var inputFile in this.files)
     {
         var reader = new StructuredStorageReader(inputFile.FullName);
         var doc    = new WordDocument(reader);
     }
 }
예제 #4
0
        public MemoryStream ConvertFromStreamToDocxMemoryStream(Stream stream)
        {
            StructuredStorageReader reader = new StructuredStorageReader(stream);
            WordDocument            doc    = new WordDocument(reader);
            var docx = WordprocessingDocument.Create("docx", DocumentType.Document);

            Converter.Convert(doc, docx);
            return(new MemoryStream(docx.CloseWithoutSavingFile()));
        }
예제 #5
0
 public WorkbookStream(string filePath)
 {
     using (var fs = new FileStream(filePath, FileMode.Open))
     {
         StructuredStorageReader ssr = new StructuredStorageReader(fs);
         var    wbStream             = ssr.GetStream("Workbook");
         byte[] wbBytes = new byte[wbStream.Length];
         wbStream.Read(wbBytes, 0, wbBytes.Length, 0);
         _biffRecords = RecordHelper.ParseBiffStreamBytes(wbBytes);
     }
 }
예제 #6
0
        public void SaveTest()
        {
            foreach (var inputFile in this.files)
            {
                var reader = new StructuredStorageReader(inputFile.FullName);
                var doc    = new WordDocument(reader);

                // Create the output DOCX
                var docx = WordprocessingDocument.Create(inputFile.FullName + "x", DocumentType.Document);
                b2xtranslator.WordprocessingMLMapping.Converter.Convert(doc, docx);
            }
        }
예제 #7
0
        public void DoTheMagic()
        {
            try
            {
                using (StructuredStorageReader reader = new StructuredStorageReader(this.Options.InputDocument))
                {
                    IStreamReader workbookReader = new VirtualStreamReader(reader.GetStream("Workbook"));

                    using (StreamWriter sw = this.Options.Mode == BiffViewerMode.File
                        ? File.CreateText(this.Options.OutputFileName)
                        : new StreamWriter(Console.OpenStandardOutput()))
                    {
                        sw.AutoFlush = true;

                        if (this.Options.PrintTextOnly)
                        {
                            PrintText(sw, workbookReader);
                        }
                        else
                        {
                            PrintHtml(sw, workbookReader);
                        }

                        if (!_isCancelled && this.Options.ShowInBrowser && this.Options.Mode == BiffViewerMode.File)
                        {
                            Util.VisitLink(this.Options.OutputFileName);
                        }
                    }
                }
            }
            catch (MagicNumberException ex)
            {
                if (this.Options.ShowErrors)
                {
                    MessageBox.Show(string.Format("This file is not a valid Excel file ({0})", ex.Message), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    Console.WriteLine(ex.ToString());
                }
            }
            catch (Exception ex)
            {
                if (this.Options.ShowErrors)
                {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    Console.WriteLine(ex.ToString());
                }
            }
        }
예제 #8
0
 private static WorkbookStream LoadDecoyDocument(string decoyDocPath)
 {
     using (var fs = new FileStream(decoyDocPath, FileMode.Open))
     {
         StructuredStorageReader ssr = new StructuredStorageReader(fs);
         var    wbStream             = ssr.GetStream("Workbook");
         byte[] wbBytes = new byte[wbStream.Length];
         wbStream.Read(wbBytes, 0, wbBytes.Length, 0);
         WorkbookStream wbs = new WorkbookStream(wbBytes);
         return(wbs);
     }
 }
예제 #9
0
        public static WorkbookStream GetMacroLoopWorkbookStream()
        {
            string template = AssemblyDirectory + Path.DirectorySeparatorChar + @"TestDocs\macro-loop.xls";

            using (var fs = new FileStream(template, FileMode.Open))
            {
                StructuredStorageReader ssr = new StructuredStorageReader(fs);
                var    wbStream             = ssr.GetStream("Workbook");
                byte[] wbBytes = new byte[wbStream.Length];
                wbStream.Read(wbBytes, 0, wbBytes.Length, 0);
                return(new WorkbookStream(wbBytes));
            }
        }
예제 #10
0
        public static byte[] GetAutoOpenTestBytes()
        {
            string template = AssemblyDirectory + Path.DirectorySeparatorChar + @"TestDocs\auto_open_test.xls";

            using (var fs = new FileStream(template, FileMode.Open))
            {
                StructuredStorageReader ssr = new StructuredStorageReader(fs);
                var    wbStream             = ssr.GetStream("Workbook");
                byte[] wbBytes = new byte[wbStream.Length];
                wbStream.Read(wbBytes, 0, wbBytes.Length, 0);
                return(wbBytes);
            }
        }
예제 #11
0
        public static WorkbookStream GetMassSelectUDFArgumentSheet()
        {
            string template = AssemblyDirectory + Path.DirectorySeparatorChar + @"TestDocs\bug_report_1_mass_select_argument.xls";

            using (var fs = new FileStream(template, FileMode.Open))
            {
                StructuredStorageReader ssr = new StructuredStorageReader(fs);
                var    wbStream             = ssr.GetStream("Workbook");
                byte[] wbBytes = new byte[wbStream.Length];
                wbStream.Read(wbBytes, 0, wbBytes.Length, 0);
                return(new WorkbookStream(wbBytes));
            }
        }
예제 #12
0
 public void TestParseability()
 {
     foreach (FileInfo inputFile in this.files)
     {
         try
         {
             StructuredStorageReader reader = new StructuredStorageReader(inputFile.FullName);
             WordDocument            doc    = new WordDocument(reader);
             Console.WriteLine("PASSED TestParseability " + inputFile.FullName);
         }
         catch (Exception e)
         {
             throw new AssertionException(e.Message + inputFile.FullName, e);
         }
     }
 }
예제 #13
0
        private static List<BiffRecord> GetDefaultMacroSheetRecords()
        {
            string defaultMacroPath = AssemblyDirectory + Path.DirectorySeparatorChar + @"default_macro_template.xls";
            using (var fs = new FileStream(defaultMacroPath, FileMode.Open))
            {
                StructuredStorageReader ssr = new StructuredStorageReader(fs);
                var wbStream = ssr.GetStream("Workbook");
                byte[] wbBytes = new byte[wbStream.Length];
                wbStream.Read(wbBytes, 0, wbBytes.Length, 0);
                WorkbookStream wbs = new WorkbookStream(wbBytes);
                //The last BOF/EOF set is our Macro sheet.
                List<BiffRecord> sheetRecords = wbs.GetRecordsForBOFRecord(wbs.GetAllRecordsByType<BOF>().Last());
                return sheetRecords;
            }

        }
예제 #14
0
        /// <summary>
        /// Converts from .doc to .docx and returns output (.docx) file name
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public string Convert(IConnectionContext context)
        {
            var outFile  = Path.GetTempPath() + Guid.NewGuid() + ".docx";
            var fileInfo = new FileInfo(context.Connection.File);

            context.Info("Converting doc to docx");

            using (var reader = new StructuredStorageReader(fileInfo.FullName)) {
                var doc               = new WordDocument(reader);
                var outType           = Converter.DetectOutputType(doc);
                var conformOutputFile = Converter.GetConformFilename(outFile, outType);
                var docx              = WordprocessingDocument.Create(conformOutputFile, outType);
                Converter.Convert(doc, docx);
            }

            return(outFile);
        }
예제 #15
0
        private static void processFile(String InputFile)
        {
            // copy processing file
            ProcessingFile procFile = new ProcessingFile(InputFile);

            //make output file name
            if (ChoosenOutputFile == null)
            {
                if (InputFile.Contains("."))
                {
                    ChoosenOutputFile = InputFile.Remove(InputFile.LastIndexOf(".")) + ".pptx";
                }
                else
                {
                    ChoosenOutputFile = InputFile + ".pptx";
                }
            }

            //open the reader
            using (StructuredStorageReader reader = new StructuredStorageReader(procFile.File.FullName))
            {
                // parse the ppt document
                PowerpointDocument ppt = new PowerpointDocument(reader);

                // detect document type and name
                OpenXmlPackage.DocumentType outType = Converter.DetectOutputType(ppt);
                string conformOutputFile            = Converter.GetConformFilename(ChoosenOutputFile, outType);

                // create the pptx document
                PresentationDocument pptx = PresentationDocument.Create(conformOutputFile, outType);

                //start time
                DateTime start = DateTime.Now;
                TraceLogger.Info("Converting file {0} into {1}", InputFile, conformOutputFile);

                // convert
                Converter.Convert(ppt, pptx);

                // stop time
                DateTime end  = DateTime.Now;
                TimeSpan diff = end.Subtract(start);
                TraceLogger.Info("Conversion of file {0} finished in {1} seconds", InputFile, diff.TotalSeconds.ToString(CultureInfo.InvariantCulture));
            }
        }
예제 #16
0
        /// <summary>
        /// Ctor
        /// </summary>
        /// <param name="file"></param>
        public XlsDocument(StructuredStorageReader reader)
        {
            this.WorkBookData = new WorkBookData();
            this.Storage      = reader;

            if (reader.FullNameOfAllStreamEntries.Contains("\\" + WORKBOOK))
            {
                this.workBookStreamReader = new VirtualStreamReader(reader.GetStream(WORKBOOK));
            }
            else if (reader.FullNameOfAllStreamEntries.Contains("\\" + ALTERNATE1))
            {
                this.workBookStreamReader = new VirtualStreamReader(reader.GetStream(ALTERNATE1));
            }
            else
            {
                throw new ExtractorException(ExtractorException.WORKBOOKSTREAMNOTFOUND);
            }

            this.workBookExtr = new WorkbookExtractor(this.workBookStreamReader, this.WorkBookData);
        }
예제 #17
0
        static void Main(string[] args)
        {
            TraceLogger.LogLevel = TraceLogger.LoggingLevel.DebugInternal;

            const string outputDir = "dumps";

            if (Directory.Exists(outputDir))
            {
                Directory.Delete(outputDir, true);
            }

            Directory.CreateDirectory(outputDir);

            string         inputFile = args[0];
            ProcessingFile procFile  = new ProcessingFile(inputFile);

            StructuredStorageReader file   = new StructuredStorageReader(procFile.File.FullName);
            PowerpointDocument      pptDoc = new PowerpointDocument(file);

            // Dump unknown records
            foreach (Record record in pptDoc)
            {
                if (record is UnknownRecord)
                {
                    string filename = String.Format(@"{0}\{1}.record", outputDir, record.GetIdentifier());

                    using (FileStream fs = new FileStream(filename, FileMode.Create))
                    {
                        record.DumpToStream(fs);
                    }
                }
            }

            // Output record tree
            Console.WriteLine(pptDoc);
            Console.WriteLine();

            // Let's make development as easy as pie.
            System.Diagnostics.Debugger.Break();
        }
예제 #18
0
        public override RadDocument Import(Stream input)
        {
            using (StructuredStorageReader structuredStorageReader = new StructuredStorageReader(input))
            {
                WordDocument wordDocument = new WordDocument(structuredStorageReader);
                OpenXmlPackage.DocumentType documentType = Converter.DetectOutputType(wordDocument);

                string tempFileName = Path.GetTempFileName();

                WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Create(tempFileName, documentType);
                Converter.Convert(wordDocument, wordprocessingDocument);

                RadDocument document;
                using (FileStream stream = File.OpenRead(tempFileName))
                {
                    document = this.docxProvider.Import(stream);
                }

                File.Delete(tempFileName);

                return(document);
            }
        }
예제 #19
0
        public override RadDocument Import(Stream input)
        {
            using (StructuredStorageReader structuredStorageReader = new StructuredStorageReader(input))
            {
                WordDocument wordDocument = new WordDocument(structuredStorageReader);
                OpenXmlPackage.DocumentType documentType = Converter.DetectOutputType(wordDocument);

                string tempFileName = Path.GetTempFileName();

                WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Create(tempFileName, documentType);
                Converter.Convert(wordDocument, wordprocessingDocument);

                RadDocument document;
                using (FileStream stream = File.OpenRead(tempFileName))
                {
                    document = this.docxProvider.Import(stream);
                }

                File.Delete(tempFileName);

                return document;
            }
        }
예제 #20
0
        public OleObject(CharacterPropertyExceptions chpx, StructuredStorageReader docStorage)
        {
            this._docStorage = docStorage;
            this.ObjectId    = getOleEntryName(chpx);

            this.Path = "\\ObjectPool\\" + this.ObjectId + "\\";
            processOleStream(this.Path + "\u0001Ole");

            if (this.fLinked)
            {
                processLinkInfoStream(this.Path + "\u0003LinkInfo");
            }
            else
            {
                processCompObjStream(this.Path + "\u0001CompObj");
            }

            //get the storage entries of this object
            this.Streams = new Dictionary <string, VirtualStream>();
            foreach (string streamname in docStorage.FullNameOfAllStreamEntries)
            {
                if (streamname.StartsWith(this.Path))
                {
                    this.Streams.Add(streamname.Substring(streamname.LastIndexOf("\\") + 1), docStorage.GetStream(streamname));
                }
            }

            //find the class if of this object
            foreach (DirectoryEntry entry in docStorage.AllEntries)
            {
                if (entry.Name == this.ObjectId)
                {
                    this.ClassId = entry.ClsId;
                    break;
                }
            }
        }
예제 #21
0
        static void Main(string[] args)
        {
            const int bytesToReadAtOnce = 512;

            char[] invalidChars = Path.GetInvalidFileNameChars();

            TraceLogger.LogLevel = TraceLogger.LoggingLevel.Error;
            ConsoleTraceListener consoleTracer = new ConsoleTraceListener();

            Trace.Listeners.Add(consoleTracer);
            Trace.AutoFlush = true;

            if (args.Length < 1)
            {
                Console.WriteLine("No parameter found. Please specify one or more compound document file(s).");
                return;
            }

            foreach (string file in args)
            {
                StructuredStorageReader storageReader = null;
                DateTime begin          = DateTime.Now;
                TimeSpan extractionTime = new TimeSpan();

                try
                {
                    // init StorageReader
                    storageReader = new StructuredStorageReader(file);

                    // read stream _entries
                    ICollection <DirectoryEntry> streamEntries = storageReader.AllStreamEntries;
                    //ICollection<DirectoryEntry> allEntries = storageReader.AllEntries;
                    //allEntries.Add(storageReader.RootDirectoryEntry);

                    List <DirectoryEntry> allEntries = new List <DirectoryEntry>();
                    allEntries.AddRange(storageReader.AllEntries);
                    allEntries.Sort(
                        delegate(DirectoryEntry a, DirectoryEntry b)
                        { return(a.Sid.CompareTo(b.Sid)); }
                        );

                    //foreach (DirectoryEntry entry in allEntries)
                    //{
                    //    Console.WriteLine(entry.Sid + ":");
                    //    Console.WriteLine("{0}: {1}", entry.Name, entry.LengthOfName);
                    //    Console.WriteLine("CLSID: " + entry.ClsId);
                    //    string hexName = "";
                    //    for (int i = 0; i < entry.Name.Length; i++)
                    //    {
                    //        hexName += String.Format("{0:X2} ", (UInt32)entry.Name[i]);
                    //    }
                    //    Console.WriteLine("{0}", hexName);
                    //    UInt32 left = entry.LeftSiblingSid;
                    //    UInt32 right = entry.RightSiblingSid;
                    //    UInt32 child = entry.ChildSiblingSid;
                    //    Console.WriteLine("{0:X02}: Left: {2:X02}, Right: {3:X02}, Child: {4:X02}, Name: {1}, Color: {5}", entry.Sid, entry.Name, (left > 0xFF) ? 0xFF : left, (right > 0xFF) ? 0xFF : right, (child > 0xFF) ? 0xFF : child, entry.Color.ToString());
                    //    Console.WriteLine("----------");
                    //    Console.WriteLine("");
                    //}

                    // create valid path names
                    Dictionary <DirectoryEntry, KeyValuePair <string, Guid> > pathNames1 = new Dictionary <DirectoryEntry, KeyValuePair <string, Guid> >();
                    foreach (DirectoryEntry entry in allEntries)
                    {
                        string name = entry.Path;
                        for (int i = 0; i < invalidChars.Length; i++)
                        {
                            name = name.Replace(invalidChars[i], '_');
                        }
                        pathNames1.Add(entry, new KeyValuePair <string, Guid>(name, entry.ClsId));
                    }


                    // Create Directory Structure
                    StructuredStorageWriter sso = new StructuredStorageWriter();
                    sso.RootDirectoryEntry.setClsId(storageReader.RootDirectoryEntry.ClsId);
                    foreach (DirectoryEntry entry in pathNames1.Keys)
                    {
                        StorageDirectoryEntry sde = sso.RootDirectoryEntry;
                        string[] storages         = entry.Path.Split(new char[] { '\\' }, StringSplitOptions.RemoveEmptyEntries);
                        for (int i = 0; i < storages.Length; i++)
                        {
                            if (entry.Type == DirectoryEntryType.STGTY_ROOT)
                            {
                                continue;
                            }
                            if (entry.Type == DirectoryEntryType.STGTY_STORAGE || i < storages.Length - 1)
                            {
                                StorageDirectoryEntry result = sde.AddStorageDirectoryEntry(storages[i]);
                                sde = (result == null) ? sde : result;
                                if (i == storages.Length - 1)
                                {
                                    sde.setClsId(entry.ClsId);
                                }
                                continue;
                            }
                            VirtualStream vstream = storageReader.GetStream(entry.Path);
                            sde.AddStreamDirectoryEntry(storages[i], vstream);
                        }
                    }

                    // Write sso to stream
                    MemoryStream myStream = new MemoryStream();
                    sso.write(myStream);

                    // Close input storage
                    storageReader.Close();
                    storageReader = null;

                    // Write stream to file

                    byte[] array = new byte[bytesToReadAtOnce];
                    int    bytesRead;


                    string outputFileName = Path.GetFileNameWithoutExtension(file) + "_output" + Path.GetExtension(file);
                    string path           = Path.GetDirectoryName(Path.GetFullPath(file));
                    outputFileName = path + "\\" + outputFileName;

                    FileStream outputFile = new FileStream(outputFileName, FileMode.Create, FileAccess.Write);
                    myStream.Seek(0, SeekOrigin.Begin);
                    do
                    {
                        bytesRead = myStream.Read(array, 0, bytesToReadAtOnce);
                        outputFile.Write(array, 0, bytesRead);
                    } while (bytesRead == array.Length);
                    outputFile.Close();


                    // --------- extract streams from written file



                    storageReader = new StructuredStorageReader(outputFileName);

                    // read stream _entries
                    streamEntries = storageReader.AllStreamEntries;

                    // create valid path names
                    Dictionary <string, string> pathNames2 = new Dictionary <string, string>();
                    foreach (DirectoryEntry entry in streamEntries)
                    {
                        string name = entry.Path;
                        for (int i = 0; i < invalidChars.Length; i++)
                        {
                            name = name.Replace(invalidChars[i], '_');
                        }
                        pathNames2.Add(entry.Path, name);
                    }

                    // create output directory
                    string outputDir = '_' + (Path.GetFileName(outputFileName)).Replace('.', '_');
                    outputDir = outputDir.Replace(':', '_');
                    outputDir = Path.GetDirectoryName(outputFileName) + "\\" + outputDir;
                    Directory.CreateDirectory(outputDir);

                    // for each stream
                    foreach (string key in pathNames2.Keys)
                    {
                        // get virtual stream by path name
                        IStreamReader streamReader = new VirtualStreamReader(storageReader.GetStream(key));

                        // read bytes from stream, write them back to disk
                        FileStream   fs     = new FileStream(outputDir + "\\" + pathNames2[key] + ".stream", FileMode.Create);
                        BinaryWriter writer = new BinaryWriter(fs);
                        array = new byte[bytesToReadAtOnce];
                        do
                        {
                            bytesRead = streamReader.Read(array);
                            writer.Write(array, 0, bytesRead);
                            writer.Flush();
                        } while (bytesRead == array.Length);

                        writer.Close();
                        fs.Close();
                    }

                    // close storage
                    storageReader.Close();
                    storageReader = null;

                    extractionTime = DateTime.Now - begin;
                    Console.WriteLine("Streams extracted in " + String.Format("{0:N2}", extractionTime.TotalSeconds) + "s. (File: " + file + ")");
                }
                catch (Exception e)
                {
                    Console.WriteLine("*** Error: " + e.Message + " (File: " + file + ")");
                    Console.WriteLine("*** StackTrace: " + e.StackTrace + " (File: " + file + ")");
                }
                finally
                {
                    if (storageReader != null)
                    {
                        storageReader.Close();
                    }
                }
            }
        }
예제 #22
0
        static void Main(string[] args)
        {
            const int bytesToReadAtOnce = 1024;

            char[] invalidChars = Path.GetInvalidFileNameChars();

            TraceLogger.LogLevel = TraceLogger.LoggingLevel.Error;
            ConsoleTraceListener consoleTracer = new ConsoleTraceListener();

            Trace.Listeners.Add(consoleTracer);
            Trace.AutoFlush = true;

            if (args.Length < 1)
            {
                Console.WriteLine("No parameter found. Please specify one or more compound document file(s).");
                return;
            }

            foreach (string file in args)
            {
                StructuredStorageReader storageReader = null;
                DateTime begin          = DateTime.Now;
                TimeSpan extractionTime = new TimeSpan();

                try
                {
                    // init StorageReader
                    storageReader = new StructuredStorageReader(file);

                    // read stream _entries
                    ICollection <DirectoryEntry> streamEntries = storageReader.AllStreamEntries;

                    // create valid path names
                    Dictionary <string, string> PathNames = new Dictionary <string, string>();
                    foreach (DirectoryEntry entry in streamEntries)
                    {
                        string name = entry.Path;
                        for (int i = 0; i < invalidChars.Length; i++)
                        {
                            name = name.Replace(invalidChars[i], '_');
                        }
                        PathNames.Add(entry.Path, name);
                    }

                    // create output directory
                    string outputDir = '_' + file.Replace('.', '_');
                    outputDir = outputDir.Replace(':', '_');
                    Directory.CreateDirectory(outputDir);

                    // for each stream
                    foreach (string key in PathNames.Keys)
                    {
                        // get virtual stream by path name
                        IStreamReader streamReader = new VirtualStreamReader(storageReader.GetStream(key));

                        // read bytes from stream, write them back to disk
                        FileStream   fs     = new FileStream(outputDir + "\\" + PathNames[key] + ".stream", FileMode.Create);
                        BinaryWriter writer = new BinaryWriter(fs);
                        byte[]       array  = new byte[bytesToReadAtOnce];
                        int          bytesRead;
                        do
                        {
                            bytesRead = streamReader.Read(array);
                            writer.Write(array, 0, bytesRead);
                            writer.Flush();
                        } while (bytesRead == array.Length);

                        writer.Close();
                        fs.Close();
                    }

                    // close storage
                    storageReader.Close();
                    storageReader = null;

                    extractionTime = DateTime.Now - begin;
                    Console.WriteLine("Streams extracted in " + String.Format("{0:N2}", extractionTime.TotalSeconds) + "s. (File: " + file + ")");
                }
                catch (Exception e)
                {
                    Console.WriteLine("*** Error: " + e.Message + " (File: " + file + ")");
                }
                finally
                {
                    if (storageReader != null)
                    {
                        storageReader.Close();
                    }
                }
            }
        }
예제 #23
0
        void Parse(StructuredStorageReader reader, int fibFC)
        {
            this.Storage            = reader;
            this.WordDocumentStream = reader.GetStream("WordDocument");

            //parse FIB
            this.WordDocumentStream.Seek(fibFC, System.IO.SeekOrigin.Begin);
            this.FIB = new FileInformationBlock(new VirtualStreamReader(this.WordDocumentStream));

            //check the file version
            if ((int)this.FIB.nFib != 0)
            {
                if (this.FIB.nFib < FileInformationBlock.FibVersion.Fib1997Beta)
                {
                    throw new ByteParseException("Could not parse the file because it was created by an unsupported application (Word 95 or older).");
                }
            }
            else
            {
                if (this.FIB.nFibNew < FileInformationBlock.FibVersion.Fib1997Beta)
                {
                    throw new ByteParseException("Could not parse the file because it was created by an unsupported application (Word 95 or older).");
                }
            }

            //get the streams
            this.TableStream = reader.GetStream(this.FIB.fWhichTblStm ? "1Table" : "0Table");

            try
            {
                this.DataStream = reader.GetStream("Data");
            }
            catch (StreamNotFoundException)
            {
                this.DataStream = null;
            }

            //Read all needed STTBs
            this.RevisionAuthorTable = new StringTable(typeof(string), this.TableStream, this.FIB.fcSttbfRMark, this.FIB.lcbSttbfRMark);
            this.FontTable           = new StringTable(typeof(FontFamilyName), this.TableStream, this.FIB.fcSttbfFfn, this.FIB.lcbSttbfFfn);
            this.BookmarkNames       = new StringTable(typeof(string), this.TableStream, this.FIB.fcSttbfBkmk, this.FIB.lcbSttbfBkmk);
            this.AutoTextNames       = new StringTable(typeof(string), this.TableStream, this.FIB.fcSttbfGlsy, this.FIB.lcbSttbfGlsy);
            //this.ProtectionUsers = new StringTable(typeof(String), this.TableStream, this.FIB.fcSttbProtUser, this.FIB.lcbSttbProtUser);
            //
            this.UserVariables = new StwStructure(this.TableStream, this.FIB.fcStwUser, this.FIB.lcbStwUser);

            //Read all needed PLCFs
            this.AnnotationsReferencePlex = new Plex <AnnotationReferenceDescriptor>(30, this.TableStream, this.FIB.fcPlcfandRef, this.FIB.lcbPlcfandRef);
            this.TextboxBreakPlex         = new Plex <BreakDescriptor>(6, this.TableStream, this.FIB.fcPlcfTxbxBkd, this.FIB.lcbPlcfTxbxBkd);
            this.TextboxBreakPlexHeader   = new Plex <BreakDescriptor>(6, this.TableStream, this.FIB.fcPlcfTxbxHdrBkd, this.FIB.lcbPlcfTxbxHdrBkd);
            this.OfficeDrawingPlex        = new Plex <FileShapeAddress>(26, this.TableStream, this.FIB.fcPlcSpaMom, this.FIB.lcbPlcSpaMom);
            this.OfficeDrawingPlexHeader  = new Plex <FileShapeAddress>(26, this.TableStream, this.FIB.fcPlcSpaHdr, this.FIB.lcbPlcSpaHdr);
            this.SectionPlex           = new Plex <SectionDescriptor>(12, this.TableStream, this.FIB.fcPlcfSed, this.FIB.lcbPlcfSed);
            this.BookmarkStartPlex     = new Plex <BookmarkFirst>(4, this.TableStream, this.FIB.fcPlcfBkf, this.FIB.lcbPlcfBkf);
            this.EndnoteReferencePlex  = new Plex <short>(2, this.TableStream, this.FIB.fcPlcfendRef, this.FIB.lcbPlcfendRef);
            this.FootnoteReferencePlex = new Plex <short>(2, this.TableStream, this.FIB.fcPlcffndRef, this.FIB.lcbPlcffndRef);
            // PLCFs without types
            this.BookmarkEndPlex = new Plex <Exception>(0, this.TableStream, this.FIB.fcPlcfBkl, this.FIB.lcbPlcfBkl);
            this.AutoTextPlex    = new Plex <Exception>(0, this.TableStream, this.FIB.fcPlcfGlsy, this.FIB.lcbPlcfGlsy);

            //read the FKPs
            this.AllPapxFkps = FormattedDiskPagePAPX.GetAllPAPXFKPs(this.FIB, this.WordDocumentStream, this.TableStream, this.DataStream);
            this.AllChpxFkps = FormattedDiskPageCHPX.GetAllCHPXFKPs(this.FIB, this.WordDocumentStream, this.TableStream);

            //read custom tables
            this.DocumentProperties            = new DocumentProperties(this.FIB, this.TableStream);
            this.Styles                        = new StyleSheet(this.FIB, this.TableStream, this.DataStream);
            this.ListTable                     = new ListTable(this.FIB, this.TableStream);
            this.ListFormatOverrideTable       = new ListFormatOverrideTable(this.FIB, this.TableStream);
            this.OfficeArtContent              = new OfficeArtContent(this.FIB, this.TableStream);
            this.HeaderAndFooterTable          = new HeaderAndFooterTable(this);
            this.AnnotationReferenceExtraTable = new AnnotationReferenceExtraTable(this.FIB, this.TableStream);
            this.CommandTable                  = new CommandTable(this.FIB, this.TableStream);
            this.AnnotationOwners              = new AnnotationOwnerList(this.FIB, this.TableStream);

            //parse the piece table and construct a list that contains all chars
            this.PieceTable = new PieceTable(this.FIB, this.TableStream);
            this.Text       = this.PieceTable.GetAllChars(this.WordDocumentStream);

            //build a dictionaries of all PAPX
            this.AllPapx = new Dictionary <int, ParagraphPropertyExceptions>();
            for (int i = 0; i < this.AllPapxFkps.Count; i++)
            {
                for (int j = 0; j < this.AllPapxFkps[i].grppapx.Length; j++)
                {
                    this.AllPapx.Add(this.AllPapxFkps[i].rgfc[j], this.AllPapxFkps[i].grppapx[j]);
                }
            }

            //build a dictionary of all SEPX
            this.AllSepx = new Dictionary <int, SectionPropertyExceptions>();
            for (int i = 0; i < this.SectionPlex.Elements.Count; i++)
            {
                //Read the SED
                var sed = (SectionDescriptor)this.SectionPlex.Elements[i];
                int cp  = this.SectionPlex.CharacterPositions[i + 1];

                //Get the SEPX
                var wordReader = new VirtualStreamReader(this.WordDocumentStream);
                this.WordDocumentStream.Seek(sed.fcSepx, System.IO.SeekOrigin.Begin);
                short cbSepx = wordReader.ReadInt16();
                var   sepx   = new SectionPropertyExceptions(wordReader.ReadBytes(cbSepx - 2));

                this.AllSepx.Add(cp, sepx);
            }

            //read the Glossary
            if (this.FIB.pnNext > 0)
            {
                this.Glossary = new WordDocument(this.Storage, (int)(this.FIB.pnNext * 512));
            }
        }
예제 #24
0
 public WordDocument(StructuredStorageReader reader, int fibFC = 0)
 {
     Parse(reader, fibFC);
 }
예제 #25
0
        static void Main(string[] args)
        {
            ParseArgs(args, ToolName);

            InitializeLogger();

            PrintWelcome(ToolName, RevisionResource);

            try
            {
                //copy processing file
                var procFile = new ProcessingFile(InputFile);

                //make output file name
                if (ChoosenOutputFile == null)
                {
                    if (InputFile.Contains("."))
                    {
                        ChoosenOutputFile = InputFile.Remove(InputFile.LastIndexOf(".")) + ".xlsx";
                    }
                    else
                    {
                        ChoosenOutputFile = InputFile + ".xlsx";
                    }
                }

                //parse the document
                using (var reader = new StructuredStorageReader(procFile.File.FullName))
                {
                    var xlsDoc = new XlsDocument(reader);

                    var    outType           = Converter.DetectOutputType(xlsDoc);
                    string conformOutputFile = Converter.GetConformFilename(ChoosenOutputFile, outType);
                    using (var spreadx = SpreadsheetDocument.Create(conformOutputFile, outType))
                    {
                        //start time
                        var start = DateTime.Now;
                        TraceLogger.Info("Converting file {0} into {1}", InputFile, conformOutputFile);

                        Converter.Convert(xlsDoc, spreadx);

                        var end  = DateTime.Now;
                        var diff = end.Subtract(start);
                        TraceLogger.Info("Conversion of file {0} finished in {1} seconds", InputFile, diff.TotalSeconds.ToString(CultureInfo.InvariantCulture));
                    }
                }
            }
            catch (ParseException ex)
            {
                TraceLogger.Error("Could not convert {0} because it was created by an unsupported application (Excel 95 or older).", InputFile);
                TraceLogger.Debug(ex.ToString());
            }
            catch (DirectoryNotFoundException ex)
            {
                TraceLogger.Error(ex.Message);
                TraceLogger.Debug(ex.ToString());
            }
            catch (FileNotFoundException ex)
            {
                TraceLogger.Error(ex.Message);
                TraceLogger.Debug(ex.ToString());
            }
            catch (Exception ex)
            {
                TraceLogger.Error("Conversion of file {0} failed.", InputFile);
                TraceLogger.Debug(ex.ToString());
            }
        }
예제 #26
0
        static void Main(string[] args)
        {
            ParseArgs(args, ToolName);

            InitializeLogger();

            PrintWelcome(ToolName, RevisionResource);

            if (CreateContextMenuEntry)
            {
                // create context menu entry
                try
                {
                    TraceLogger.Info("Creating context menu entry for xls2x ...");
                    RegisterForContextMenu(GetContextMenuKey(ContextMenuInputExtension, ContextMenuText));
                    TraceLogger.Info("Succeeded.");
                }
                catch (Exception)
                {
                    TraceLogger.Info("Failed. Sorry :(");
                }
            }
            else
            {
                try
                {
                    //copy processing file
                    ProcessingFile procFile = new ProcessingFile(InputFile);

                    //make output file name
                    if (ChoosenOutputFile == null)
                    {
                        if (InputFile.Contains("."))
                        {
                            ChoosenOutputFile = InputFile.Remove(InputFile.LastIndexOf(".")) + ".xlsx";
                        }
                        else
                        {
                            ChoosenOutputFile = InputFile + ".xlsx";
                        }
                    }

                    //parse the document
                    using (StructuredStorageReader reader = new StructuredStorageReader(procFile.File.FullName))
                    {
                        XlsDocument xlsDoc = new XlsDocument(reader);

                        OpenXmlPackage.DocumentType outType = Converter.DetectOutputType(xlsDoc);
                        string conformOutputFile            = Converter.GetConformFilename(ChoosenOutputFile, outType);
                        using (SpreadsheetDocument spreadx = SpreadsheetDocument.Create(conformOutputFile, outType))
                        {
                            //start time
                            DateTime start = DateTime.Now;
                            TraceLogger.Info("Converting file {0} into {1}", InputFile, conformOutputFile);

                            Converter.Convert(xlsDoc, spreadx);

                            DateTime end  = DateTime.Now;
                            TimeSpan diff = end.Subtract(start);
                            TraceLogger.Info("Conversion of file {0} finished in {1} seconds", InputFile, diff.TotalSeconds.ToString(CultureInfo.InvariantCulture));
                        }
                    }
                }
                catch (ParseException ex)
                {
                    TraceLogger.Error("Could not convert {0} because it was created by an unsupported application (Excel 95 or older).", InputFile);
                    TraceLogger.Debug(ex.ToString());
                }
                catch (DirectoryNotFoundException ex)
                {
                    TraceLogger.Error(ex.Message);
                    TraceLogger.Debug(ex.ToString());
                }
                catch (FileNotFoundException ex)
                {
                    TraceLogger.Error(ex.Message);
                    TraceLogger.Debug(ex.ToString());
                }
                catch (ZipCreationException ex)
                {
                    TraceLogger.Error("Could not create output file {0}.", ChoosenOutputFile);
                    TraceLogger.Debug(ex.ToString());
                }
                catch (Exception ex)
                {
                    TraceLogger.Error("Conversion of file {0} failed.", InputFile);
                    TraceLogger.Debug(ex.ToString());
                }
            }
        }
예제 #27
0
 public WordDocument(StructuredStorageReader reader)
 {
     parse(reader, 0);
 }
예제 #28
0
        public static void Main(string[] args)
        {
            ParseArgs(args, ToolName);

            InitializeLogger();

            PrintWelcome(ToolName, RevisionResource);

            // convert
            try
            {
                //copy processing file
                var procFile = new ProcessingFile(InputFile);

                //make output file name
                if (ChoosenOutputFile == null)
                {
                    if (InputFile.Contains("."))
                    {
                        ChoosenOutputFile = InputFile.Remove(InputFile.LastIndexOf(".")) + ".docx";
                    }
                    else
                    {
                        ChoosenOutputFile = InputFile + ".docx";
                    }
                }

                //open the reader
                using (var reader = new StructuredStorageReader(procFile.File.FullName))
                {
                    //parse the input document
                    var doc = new WordDocument(reader);

                    //prepare the output document
                    var    outType           = Converter.DetectOutputType(doc);
                    string conformOutputFile = Converter.GetConformFilename(ChoosenOutputFile, outType);
                    var    docx = WordprocessingDocument.Create(conformOutputFile, outType);

                    //start time
                    var start = DateTime.Now;
                    TraceLogger.Info("Converting file {0} into {1}", InputFile, conformOutputFile);

                    //convert the document
                    Converter.Convert(doc, docx);

                    var end  = DateTime.Now;
                    var diff = end.Subtract(start);
                    TraceLogger.Info("Conversion of file {0} finished in {1} seconds", InputFile, diff.TotalSeconds.ToString(CultureInfo.InvariantCulture));
                }
            }
            catch (DirectoryNotFoundException ex)
            {
                TraceLogger.Error(ex.Message);
                TraceLogger.Debug(ex.ToString());
            }
            catch (FileNotFoundException ex)
            {
                TraceLogger.Error(ex.Message);
                TraceLogger.Debug(ex.ToString());
            }
            catch (ReadBytesAmountMismatchException ex)
            {
                TraceLogger.Error("Input file {0} is not a valid Microsoft Word 97-2003 file.", InputFile);
                TraceLogger.Debug(ex.ToString());
            }
            catch (MagicNumberException ex)
            {
                TraceLogger.Error("Input file {0} is not a valid Microsoft Word 97-2003 file.", InputFile);
                TraceLogger.Debug(ex.ToString());
            }
            catch (UnspportedFileVersionException ex)
            {
                TraceLogger.Error("File {0} has been created with a Word version older than Word 97.", InputFile);
                TraceLogger.Debug(ex.ToString());
            }
            catch (ByteParseException ex)
            {
                TraceLogger.Error("Input file {0} is not a valid Microsoft Word 97-2003 file.", InputFile);
                TraceLogger.Debug(ex.ToString());
            }
            catch (MappingException ex)
            {
                TraceLogger.Error("There was an error while converting file {0}: {1}", InputFile, ex.Message);
                TraceLogger.Debug(ex.ToString());
            }
            catch (Exception ex)
            {
                TraceLogger.Error("Conversion of file {0} failed.", InputFile);
                TraceLogger.Debug(ex.ToString());
            }
        }
예제 #29
0
 public WordDocument(StructuredStorageReader reader, Int32 fibFC)
 {
     parse(reader, fibFC);
 }
예제 #30
0
 public void SetUp()
 {
     this.reader = new StructuredStorageReader(this.file);
     this.doc    = new WordDocument(this.reader);
 }
예제 #31
0
        public PowerpointDocument(StructuredStorageReader file)
        {
            try
            {
                this.CurrentUserStream = file.GetStream("Current User");
                var rec = Record.ReadRecord(this.CurrentUserStream);
                if (rec is CurrentUserAtom)
                {
                    this.CurrentUserAtom = (CurrentUserAtom)rec;
                }
                else
                {
                    this.CurrentUserStream.Position = 0;
                    var bytes = new byte[this.CurrentUserStream.Length];
                    this.CurrentUserStream.Read(bytes);
                    string s = Encoding.UTF8.GetString(bytes).Replace("\0", "");
                }
            }
            catch (InvalidRecordException e)
            {
                throw new InvalidStreamException("Current user stream is not valid", e);
            }

            // Optional 'Pictures' stream
            if (file.FullNameOfAllStreamEntries.Contains("\\Pictures"))
            {
                try
                {
                    this.PicturesStream    = file.GetStream("Pictures");
                    this.PicturesContainer = new Pictures(new BinaryReader(this.PicturesStream), (uint)this.PicturesStream.Length, 0, 0, 0);
                }
                catch (InvalidRecordException e)
                {
                    throw new InvalidStreamException("Pictures stream is not valid", e);
                }
            }


            this.PowerpointDocumentStream = file.GetStream("PowerPoint Document");

            try
            {
                this.DocumentSummaryInformationStream = file.GetStream("DocumentSummaryInformation");
                ScanDocumentSummaryInformation();
            }
            catch (StructuredStorage.Common.StreamNotFoundException)
            {
                //ignore
            }


            if (this.CurrentUserAtom != null)
            {
                this.PowerpointDocumentStream.Seek(this.CurrentUserAtom.OffsetToCurrentEdit, SeekOrigin.Begin);
                this.LastUserEdit = (UserEditAtom)Record.ReadRecord(this.PowerpointDocumentStream);
            }

            this.ConstructPersistObjectDirectory();

            this.IdentifyDocumentPersistObject();
            this.IdentifyMasterPersistObjects();
            this.IdentifySlidePersistObjects();
            this.IdentifyOlePersistObjects();
            this.IdentifyVbaProjectObject();
        }