示例#1
0
        //public async Task<bool> ProcessZipFile(Uri uri)
        //{
        //    // using (var outputZip = new ZipArchive(outputStream, ZipArchiveMode.Create, leaveOpen: true))

        //    var stopWatch = new Stopwatch();
        //    stopWatch.Start();
        //    ZipArchive outputZip = null;

        //    CSVMapping csvMapping = null;
        //    var streamParser = new StreamParser();

        //    var file = _downloader.GetFile(uri);
        //    var zip = _zipProcessor.UnZip(file);


        //    if (zip.Entries.Any())
        //    {
        //        foreach (var zipEntry in zip.Entries)
        //        {
        //            if (!zipEntry.FullName.ToLower().EndsWith(".xml")) continue;
        //            using (var inputStream = zipEntry.Open())
        //            using (var stream = new MemoryStream())
        //            {
        //                _transformer.Transform(inputStream, stream);
        //                if (stream.Length == 0) continue;

        //                csvMapping = ProcessZipEntry(csvMapping, streamParser, stream, out outputZip);
        //            }
        //        }

        //        // Add metadata to each archive
        //        if (csvMapping != null)
        //        {
        //            CSVMappingHelper.AddMetadata(outputZip, csvMapping);
        //        }
        //    }

        //    stopWatch.Stop();

        //    var ts = stopWatch.Elapsed;
        //    var elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
        //        ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);

        //    using (var containerBuilder = new ContainerBuilder(outputZip))
        //    {



        //    }

        //    return false;
        //}

        //private CSVMapping ProcessZipEntry(CSVMapping csvMapping
        //    , StreamParser streamParser
        //    , MemoryStream stream
        //    , out ZipArchive outputZip)
        //{
        //    stream.Position = 0;
        //    var data = streamParser.Parse(stream).Result;

        //    csvMapping = _mappingProvider.GetMappingByData(data);
        //    outputZip = new ZipArchive()

        //    // var serializer = new CsvSerializer(containerBuilder, csvMapping);
        //    // await serializer.Serialize(data.ParsedEntities);

        //    return csvMapping;
        //}


        public async Task ProcessAsync(Uri uri, Stream outputStream)
        {
            // , File.Create($"{Guid.NewGuid().ToString().Substring(0,5)}.zip"

            using (var outputZip = new ZipArchive(outputStream, ZipArchiveMode.Create, leaveOpen: true))
                using (var containerBuilder = new ContainerBuilder(outputZip))
                {
                    var stopWatch = new Stopwatch();
                    stopWatch.Start();

                    CSVMapping csvMapping   = null;
                    var        streamParser = new StreamParser();

                    var file = _downloader.GetFile(uri);
                    var zip  = _zipProcessor.UnZip(file);


                    if (zip.Entries.Any())
                    {
                        foreach (var zipEntry in zip.Entries)
                        {
                            if (!zipEntry.FullName.ToLower().EndsWith(".xml"))
                            {
                                continue;
                            }
                            using (var inputStream = zipEntry.Open())
                                using (var stream = new MemoryStream())
                                {
                                    _transformer.Transform(inputStream, stream);
                                    if (stream.Length == 0)
                                    {
                                        continue;
                                    }
                                    stream.Position = 0;
                                    var data = await streamParser.Parse(stream);

                                    csvMapping = _mappingProvider.GetMappingByData(data);
                                    var serializer = new CsvSerializer(containerBuilder, csvMapping);

                                    await serializer.Serialize(data.ParsedEntities);
                                }
                        }

                        // Add metadata to each archive
                        if (csvMapping != null)
                        {
                            CSVMappingHelper.AddMetadata(outputZip, csvMapping);
                        }
                    }

                    stopWatch.Stop();

                    var ts          = stopWatch.Elapsed;
                    var elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
                                                    ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
                }
        }
示例#2
0
        public async Task SomeMajorTask()
        {
            var outputStream = new MemoryStream();

            using (var outputZip = new ZipArchive(outputStream, ZipArchiveMode.Create, leaveOpen: true))
                using (var containerBuilder = new ContainerBuilder(outputZip))
                {
                    var stopWatch = new Stopwatch();
                    stopWatch.Start();

                    CSVMapping csvMapping   = null;
                    var        streamParser = new StreamParser();

                    var strFilePath = @"C:\Projects\assetfiles\testfile.zip";

                    var fs = new FileStream(strFilePath, FileMode.Open, FileAccess.Read);
                    var ms = new MemoryStream();
                    fs.CopyTo(ms);

                    //
                    // var file = _downloader.GetFile(uri);
                    //var zip = _zipProcessor.UnZip(file);

                    var zip = new ZipArchive(ms, ZipArchiveMode.Read, true);

                    foreach (var zipEntry in zip.Entries)
                    {
                        if (!zipEntry.FullName.ToLower().EndsWith(".xml"))
                        {
                            continue;
                        }
                        using (var inputStream = zipEntry.Open())
                            using (var stream = new MemoryStream())
                            {
                                _transformer.Transform(inputStream, stream);
                                if (stream.Length == 0)
                                {
                                    continue;
                                }
                                stream.Position = 0;
                                var data = await streamParser.Parse(stream);

                                csvMapping = _mappingProvider.GetMappingByData(data);
                                var serializer = new CsvSerializer(containerBuilder, csvMapping);

                                await serializer.Serialize(data.ParsedEntities);
                            }
                    }

                    // Add metadata to each archive
                    if (csvMapping != null)
                    {
                        CSVMappingHelper.AddMetadata(outputZip, csvMapping);
                    }

                    stopWatch.Stop();

                    var ts          = stopWatch.Elapsed;
                    var elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
                                                    ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
                }
        }