Exemplo n.º 1
0
        /// <summary>
        /// Create a CFStorage using an existing directory (previously loaded).
        /// </summary>
        /// <param name="compFile">The Storage Owner - CompoundFile</param>
        /// <param name="dirEntry">An existing Directory Entry</param>
        internal ReadonlyCompoundFileStorage(ReadonlyCompoundFile compFile, IDirectoryEntry dirEntry)
            : base(compFile)
        {
            if (dirEntry == null || dirEntry.SID < 0)
            {
                throw new CFException("Attempting to create a CFStorage using an unitialized directory");
            }

            this.DirEntry = dirEntry;
        }
Exemplo n.º 2
0
        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            var file = new FileInfo("Test.pptx");

            var tf = @"F:\temp\KewalnaidiNaborereefal.pptx";

            if (File.Exists(tf))
            {
                file = new FileInfo(tf);
            }

            long lastAllocatedBytesForCurrentThread;

            using var presentationDocument = PresentationDocument.Open(file.FullName, false);
            var slide = presentationDocument.PresentationPart !.SlideParts.First().Slide;

            var graphicFrame     = slide.CommonSlideData !.ShapeTree !.GetFirstChild <GraphicFrame>() !;
            var graphic          = graphicFrame.Graphic !;
            var graphicData      = graphic.GraphicData !;
            var alternateContent = graphicData.GetFirstChild <AlternateContent>() !;
            var choice           = alternateContent.GetFirstChild <AlternateContentChoice>() !;
            var oleObject        = choice.GetFirstChild <OleObject>() !;

            Debug.Assert(oleObject.GetFirstChild <OleObjectEmbed>() != null);
            var id   = oleObject.Id !;
            var part = slide.SlidePart !.GetPartById(id !);

            Debug.Assert(part.ContentType == "application/vnd.openxmlformats-officedocument.oleObject");

            var allocatedBytesForCurrentThread = GC.GetAllocatedBytesForCurrentThread();
            var s = part.GetStream();

            lastAllocatedBytesForCurrentThread = GC.GetAllocatedBytesForCurrentThread();
            Debug.WriteLine($"GetStream {lastAllocatedBytesForCurrentThread - allocatedBytesForCurrentThread}");
            allocatedBytesForCurrentThread = lastAllocatedBytesForCurrentThread;

            var tempFolder = @"F:\temp";

            if (!Directory.Exists(tempFolder))
            {
                tempFolder = System.IO.Path.GetTempPath();
            }

            tempFolder = System.IO.Path.Combine(tempFolder, System.IO.Path.GetRandomFileName());
            Directory.CreateDirectory(tempFolder);
            //CompoundFileUnzipper.Unzip(s, tempFolder, byteArrayPool);

            // 为了性能考虑,不再使用内存方式,全部写入到文件
            //var forwardSeekStream = new ForwardSeekStream(s, byteArrayPool);
            var oleFile = System.IO.Path.Combine(tempFolder, System.IO.Path.GetRandomFileName());

            using var oleFileStream = new FileStream(oleFile, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.None,
                                                     bufferSize: 4096, FileOptions.RandomAccess);
            s.CopyTo(oleFileStream);
            oleFileStream.Position = 0;

            //Origin(forwardSeekStream);
            var cf = new ReadonlyCompoundFile(oleFileStream);

            lastAllocatedBytesForCurrentThread = GC.GetAllocatedBytesForCurrentThread();
            Debug.WriteLine($"CompoundFile {lastAllocatedBytesForCurrentThread - allocatedBytesForCurrentThread}");
            allocatedBytesForCurrentThread = lastAllocatedBytesForCurrentThread;

            var packageStream = cf.RootStorage.GetStream("Package");

            lastAllocatedBytesForCurrentThread = GC.GetAllocatedBytesForCurrentThread();
            Debug.WriteLine($"GetStream {lastAllocatedBytesForCurrentThread - allocatedBytesForCurrentThread}");
            allocatedBytesForCurrentThread = lastAllocatedBytesForCurrentThread;

            //var tempFolder = @"F:\temp";
            //if (!Directory.Exists(tempFolder))
            //{
            //    tempFolder = System.IO.Path.GetTempPath();
            //}

            var xlsxFile = System.IO.Path.Combine(tempFolder, System.IO.Path.GetRandomFileName() + ".xlsx");

            using (var fileStream = File.OpenWrite(xlsxFile))
            {
                //fileStream.Write(packageStream.GetData().AsSpan());
                cf.CopyTo(packageStream, fileStream);
            }

            //var fakeStream = new FakeStream();
            //cf.CopyTo(packageStream, fakeStream, byteArrayPool);

            //using (var fileStream = File.OpenWrite(xlsxFile))
            //{
            //    fileStream.Write(packageStream.GetData().AsSpan());
            //}
            //using var spreadsheetDocument = SpreadsheetDocument.Open(xlsxFile, false);
            //var sheets = spreadsheetDocument.WorkbookPart!.Workbook.Sheets;

            lastAllocatedBytesForCurrentThread = GC.GetAllocatedBytesForCurrentThread();
            Debug.WriteLine($"CopyTo {lastAllocatedBytesForCurrentThread - allocatedBytesForCurrentThread}");
            allocatedBytesForCurrentThread = lastAllocatedBytesForCurrentThread;



            //// OpenMcdf.CFException:“Cannot load a non-seekable Stream”
            //var compoundFile = new CompoundFile(part.GetStream(FileMode.Open));

            //var oleFile = System.IO.Path.Combine(tempFolder, System.IO.Path.GetRandomFileName());
            //using (var fileStream = File.OpenWrite(oleFile))
            //{
            //    using var stream = part.GetStream(FileMode.Open);
            //    stream.CopyTo(fileStream);
            //}

            //var compoundFile = new CompoundFile(oleFile);
            //var packageStream = compoundFile.RootStorage.GetStream("Package");
            //var xlsxFile = System.IO.Path.Combine(tempFolder, System.IO.Path.GetRandomFileName() + ".xlsx");
            //using (var fileStream = File.OpenWrite(xlsxFile))
            //{
            //    fileStream.Write(packageStream.GetData().AsSpan());
            //}

            using var spreadsheetDocument = SpreadsheetDocument.Open(xlsxFile, false);
            var sheets = spreadsheetDocument.WorkbookPart !.Workbook.Sheets;
        }
Exemplo n.º 3
0
 protected ReadonlyCompoundFileItem(ReadonlyCompoundFile compoundFile)
 {
     this.CompoundFile = compoundFile;
 }