private static void AddUpdateDeleteInventories(ZipArchiveEntry inventoriesEntry) { Console.WriteLine("inventories.csv is {0} bytes", inventoriesEntry.Length); IEnumerable<Inventory> inventories = null; using (var entryStream = inventoriesEntry.Open()) { Console.WriteLine("Starting inventories at {0:hh:mm:ss.fff}", DateTime.Now); var stopwatch = new Stopwatch(); stopwatch.Start(); var reader = new StreamReader(entryStream); var csv = new CsvReader(reader); csv.Configuration.RegisterClassMap(new InventoryMap()); StringBuilder invForImport = new StringBuilder(); inventories = csv.GetRecords<Inventory>().Where(i => !i.IsDead); var inventoryCollection = new InventoryCollection(); inventoryCollection.SetItems(inventories); var importer = new NpgsqlBulkImporter(ConfigurationManager.ConnectionStrings["spurious"].ConnectionString, stopwatch); importer.BulkImport("inventories", inventoryCollection); stopwatch.Stop(); Console.WriteLine("Finished inventories at {0:hh:mm:ss.fff}, taking {1}", DateTime.Now, stopwatch.Elapsed); } }
static void WriteZipArchiveEntry(ZipArchiveEntry entry, string toWrite) { using (StreamWriter writer = new StreamWriter(entry.Open())) { writer.Write(toWrite); } }
/// <summary> /// Reads a ZipArchive entry as the routes CSV and extracts the route colors. /// </summary> private static List<GoogleRoute> ParseRouteCSV(ZipArchiveEntry entry) { var routes = new List<GoogleRoute>(); using (var reader = new StreamReader(entry.Open())) { // Ignore the format line reader.ReadLine(); while (!reader.EndOfStream) { var parts = reader.ReadLine().Split(','); // Ignore all routes which aren't part of CTS and thus don't have any real-time data. if (parts[0].Contains("ATS") || parts[0].Contains("PC") || parts[0].Contains("LBL")) { continue; } routes.Add(new GoogleRoute(parts)); } } return routes; }
private static bool AreHashEqual(FileInfo existing, ZipArchiveEntry duplicate) { byte[] originalHash; byte[] otherHash; try { using (FileStream original = File.Open(existing.FullName, FileMode.Open, FileAccess.Read)) { if (null == (originalHash = HashFile(original))) { return false; } } using (Stream other = duplicate.Open()) { if (null == (otherHash = HashFile(other))) { return false; } } if (originalHash.Length == otherHash.Length) { for (int index = 0; index < originalHash.Length; index++) { if (originalHash[index] != otherHash[index]) { WriteError("Hashes don't match."); return false; } } return true; } return false; } catch (Exception e) { WriteError("Error while trying to compare hash. Error {0}", e.Message); return false; } }
internal static string Process(ZipArchiveEntry packageFile, IMSBuildNuGetProjectSystem msBuildNuGetProjectSystem) { using (var stream = packageFile.Open()) { return Process(stream, msBuildNuGetProjectSystem, throwIfNotFound: false); } }
private static void AddUpdateDeleteStores(ZipArchiveEntry entry) { IEnumerable<Store> stores = null; using (var entryStream = entry.Open()) { Console.WriteLine("stores.csv is {0} bytes", entry.Length); Console.WriteLine("Starting stores at {0:hh:mm:ss.fff}", DateTime.Now); var storeTimer = new Stopwatch(); storeTimer.Start(); var reader = new StreamReader(entryStream); var csv = new CsvReader(reader); csv.Configuration.RegisterClassMap(new StoreMap()); stores = csv.GetRecords<Store>().Where(s => !s.IsDead); var storesCollection = new StoreCollection(); storesCollection.SetItems(stores); var importer = new NpgsqlBulkImporter(ConfigurationManager.ConnectionStrings["spurious"].ConnectionString, storeTimer); importer.BulkImport("stores", storesCollection); using (var wrapper = new NpgsqlConnectionWrapper(ConfigurationManager.ConnectionStrings["spurious"].ConnectionString)) { wrapper.Connection.Open(); var rowsGeoUpdated = wrapper.ExecuteNonQuery("update stores s set (location) = ((select ST_SetSRID(ST_MakePoint(longitude, latitude), 4326) from stores ss where s.id = ss.id))"); Console.WriteLine($"Updated {rowsGeoUpdated} rows geo data from lat/long data"); } storeTimer.Stop(); Console.WriteLine("Finished stores at {0:hh:mm:ss.fff}, taking {1}", DateTime.Now, storeTimer.Elapsed); } }
private async Task Extract(ZipArchiveEntry entry, string basePath) { var fn = Path.Combine(basePath, entry.FullName); using (var fstream = File.Create(fn)) { await entry.Open().CopyToAsync(fstream); } }
/// <summary> /// Unzips ZipArchiveEntry asynchronously. /// </summary> /// <param name="entry">The entry which needs to be unzipped</param> /// <param name="filePath">The entry's full name</param> /// <param name="unzipFolder">The unzip folder</param> /// <returns></returns> public static async Task UnzipZipArchiveEntryAsync(ZipArchiveEntry entry, string filePath, StorageFolder unzipFolder) { if (IfPathContainDirectory(filePath)) { // Create sub folder string subFolderName = Path.GetDirectoryName(filePath); bool isSubFolderExist = await IfFolderExistsAsync(unzipFolder, subFolderName); StorageFolder subFolder; if (!isSubFolderExist) { // Create the sub folder. subFolder = await unzipFolder.CreateFolderAsync(subFolderName, CreationCollisionOption.ReplaceExisting); } else { // Just get the folder. subFolder = await unzipFolder.GetFolderAsync(subFolderName); } // All sub folders have been created. Just pass the file name to the Unzip function. string newFilePath = Path.GetFileName(filePath); if (!string.IsNullOrEmpty(newFilePath)) { // Unzip file iteratively. await UnzipZipArchiveEntryAsync(entry, newFilePath, subFolder); } } else { // Read uncompressed contents using (Stream entryStream = entry.Open()) { byte[] buffer = new byte[entry.Length]; entryStream.Read(buffer, 0, buffer.Length); // Create a file to store the contents StorageFile uncompressedFile = await unzipFolder.CreateFileAsync (entry.Name, CreationCollisionOption.ReplaceExisting); // Store the contents using (IRandomAccessStream uncompressedFileStream = await uncompressedFile.OpenAsync(FileAccessMode.ReadWrite)) { using (Stream outstream = uncompressedFileStream.AsStreamForWrite()) { outstream.Write(buffer, 0, buffer.Length); outstream.Flush(); } } } } }
private PackageManifest _TryGetPackageManifest(ZipArchiveEntry manifestEntry) { using (var reader = manifestEntry.Open()) using (var xmlReader = XmlReader.Create(reader, new XmlReaderSettings { Schemas = SchemaSets.PackageManifest })) { if (!mPackageManifestSerializer.CanDeserialize(xmlReader)) { return null; } return (PackageManifest)mPackageManifestSerializer.Deserialize(xmlReader); } }
private static async Task InflateEntryAsync(ZipArchiveEntry entry, StorageFolder destFolder, bool createSub = false) { string filePath = entry.FullName; if (!string.IsNullOrEmpty(filePath) && filePath.Contains("/") && !createSub) { // Create sub folder string subFolderName = Path.GetDirectoryName(filePath); StorageFolder subFolder; // Create or return the sub folder. subFolder = await destFolder.CreateFolderAsync(subFolderName, CreationCollisionOption.OpenIfExists); string newFilePath = Path.GetFileName(filePath); if (!string.IsNullOrEmpty(newFilePath)) { // Unzip file iteratively. await InflateEntryAsync(entry, subFolder, true); } } else { // Read uncompressed contents using (Stream entryStream = entry.Open()) { byte[] buffer = new byte[entry.Length]; entryStream.Read(buffer, 0, buffer.Length); // Create a file to store the contents StorageFile uncompressedFile = await destFolder.CreateFileAsync(entry.Name, CreationCollisionOption.ReplaceExisting); // Store the contents using (IRandomAccessStream uncompressedFileStream = await uncompressedFile.OpenAsync(FileAccessMode.ReadWrite)) { using (Stream outstream = uncompressedFileStream.AsStreamForWrite()) { outstream.Write(buffer, 0, buffer.Length); outstream.Flush(); } } } } }
protected void CreateFileZip() { string dirRoot = Server.MapPath("~/file"); //get a list of files string[] filesToZip = Directory.GetFiles(dirRoot, "*.*", SearchOption.AllDirectories); using (MemoryStream zipMS = new MemoryStream()) { using (System.IO.Compression.ZipArchive zipArchive = new System.IO.Compression.ZipArchive(zipMS, System.IO.Compression.ZipArchiveMode.Create, true)) { //loop through files to add foreach (string fileToZip in filesToZip) { //exclude some files? -I don't want to ZIP other .zips in the folder. if (new FileInfo(fileToZip).Extension == ".zip") { continue; } //exclude some file names maybe? if (fileToZip.Contains("node_modules")) { continue; } //read the file bytes byte[] fileToZipBytes = System.IO.File.ReadAllBytes(fileToZip); //create the entry - this is the zipped filename //change slashes - now it's VALID System.IO.Compression.ZipArchiveEntry zipFileEntry = zipArchive.CreateEntry(fileToZip.Replace(dirRoot, "").Replace('\\', '/')); //add the file contents using (Stream zipEntryStream = zipFileEntry.Open()) using (BinaryWriter zipFileBinary = new BinaryWriter(zipEntryStream)) { zipFileBinary.Write(fileToZipBytes); } } } using (FileStream finalZipFileStream = new FileStream(Server.MapPath("~/file/DanhSachBangKe.zip"), FileMode.Create)) { zipMS.Seek(0, SeekOrigin.Begin); zipMS.CopyTo(finalZipFileStream); } } }
private static void AddUpdateDeleteProducts(ZipArchiveEntry entry) { IEnumerable<Product> products = null; using (var entryStream = entry.Open()) { Console.WriteLine("products.csv is {0} bytes", entry.Length); Console.WriteLine("Starting products at {0:hh:mm:ss.fff}", DateTime.Now); var stopwatch = new Stopwatch(); stopwatch.Start(); var reader = new StreamReader(entryStream); var csv = new CsvReader(reader); csv.Configuration.RegisterClassMap(new ProductMap()); products = csv.GetRecords<Product>().Where(p => !p.IsDead); var productCollection = new ProductCollection(); productCollection.SetItems(products); var importer = new NpgsqlBulkImporter(ConfigurationManager.ConnectionStrings["spurious"].ConnectionString, stopwatch); importer.BulkImport("products", productCollection); stopwatch.Stop(); Console.WriteLine("Finished products at {0:hh:mm:ss.fff}, taking {1}", DateTime.Now, stopwatch.Elapsed); } }
private async Task<List<Dictionary<String, String>>> ReadSheetData(ZipArchiveEntry worksheet) { List<Dictionary<String, String>> sheetDataSet = null; await Task.Run(() => { try { if (worksheet != null) { try{ excelReader.headers.Clear(); } catch { } try{ excelReader.rowIdentifierList.Clear(); } catch { } sheetDataSet = new List<Dictionary<string, string>>(); using (var sr = worksheet.Open()) { XDocument xdoc = XDocument.Load(sr); //get element to first sheet data XNamespace xmlns = "http://schemas.openxmlformats.org/spreadsheetml/2006/main"; XElement sheetData = xdoc.Root.Element(xmlns + "sheetData"); //build header and row defintion list var firstRow = sheetData.Elements().First(); foreach (var c in firstRow.Elements()) { //the c element, if have attribute t, will need to consult sharedStrings string val = c.Elements().First().Value; if (c.Attribute("t") != null) { excelReader.headers.Add(_sharedStrings[Convert.ToInt32(val)]); } else { excelReader.headers.Add(val); } // Row Identifiers will helpfull in tracking the empty cells var rowAttribute = c.Attribute("r"); if (rowAttribute != null) { String rowAttributeValue = rowAttribute.Value; int pos = Regex.Match(rowAttributeValue, "[0-9]").Index; rowAttributeValue = rowAttributeValue.Substring(0, pos); excelReader.rowIdentifierList.Add(rowAttributeValue); } } foreach (var row in sheetData.Elements()) { //skip row 1 if (row.Attribute("r").Value == "1") continue; Dictionary<string, string> rowData = new Dictionary<string, string>(); int i = 0; var elementsToLookup = row.Elements(); for (int rowResolverIndex = 0; rowResolverIndex < elementsToLookup.Count(); rowResolverIndex++) { var c = elementsToLookup.ElementAt(rowResolverIndex); // Get the row number of the cell to check whether we are missing out any entry // due to empty data var rowAttribute = c.Attribute("r"); if (rowAttribute != null) { String actualRowIdentifier = rowAttribute.Value; int pos = Regex.Match(actualRowIdentifier, "[0-9]").Index; actualRowIdentifier = actualRowIdentifier.Substring(0, pos); // Get the element from rowIdentifierList for the index for (int lookupIndex = i; lookupIndex < excelReader.rowIdentifierList.Count(); lookupIndex++) { String expectedRowIdentifier = excelReader.rowIdentifierList.ElementAt(lookupIndex); if (expectedRowIdentifier.Equals(actualRowIdentifier)) { break; } else { String keyToIndex = headers[lookupIndex]; if (!rowData.ContainsKey(keyToIndex)) { rowData.Add(keyToIndex, ""); // Placing an empty data for the particular header i++; } } } } //down to each c element // Check for element value v and get the value string val = String.Empty; //-- Fails when formula / formatting options are available String temp = String.Empty; var elements = c.Elements(); if (elements.Count() > 0) { temp = elements.First().Value; } else { // Invalid/No data } var _matchedElementsList = elements.Where(element => element.Name.LocalName.Equals("v")); var _element = String.Empty; if (_matchedElementsList.Count() > 0) { val = _matchedElementsList.FirstOrDefault().Value; } else { // Invalid/No data } // Get the value after checking if (c.Attribute("t") != null) { var valueOfStringType = c.Attribute("t").Value; // Check whether it is "s" / "e" // Do this if it is s if (valueOfStringType.Equals("s")) { rowData.Add(excelReader.headers[i], _sharedStrings[Convert.ToInt32(val)]); } else if (valueOfStringType.Equals("e")) { rowData.Add(excelReader.headers[i], temp); } } else if (c.Attribute("s") != null) { // Date is stored in excel as days passed since January 1 1990 // Logic for converting the long values (days since january 1 1990) //Excel manages the date in older system // Refer this link : http://stackoverflow.com/questions/11968570/converting-double-to-datetime // One Fix is to subtract 2 from the value // Or using the //DateTime myDate = DateTime.FromOADate(41172); -- TODO : Check this String dateValueAsString = String.Empty; if (!String.IsNullOrEmpty(val)) { long daysSince1990 = Convert.ToInt64(Convert.ToDouble(val) - 2); // Val - extracted from excel sheet DateTime startDate = new DateTime(1900, 01, 01); var date = startDate.AddDays(daysSince1990); dateValueAsString = date.ToString("MM/dd/yyyy"); // Specifying the format for displaying the date as string } rowData.Add(excelReader.headers[i], dateValueAsString); } else { rowData.Add(excelReader.headers[i], val); } i++; } for (int escapedEntriesIndex = i; escapedEntriesIndex < excelReader.rowIdentifierList.Count; escapedEntriesIndex++) { String escapedKey = excelReader.headers.ElementAt(escapedEntriesIndex); if (!String.IsNullOrEmpty(escapedKey)) { if (!rowData.ContainsKey(escapedKey)) { rowData.Add(escapedKey, ""); } } } sheetDataSet.Add(rowData); } } } } catch (Exception exception) { throw exception; } }); return sheetDataSet; }
// Extract a single zip archive entry to the given folder private async Task ExtractZipEntryAsync(ZipArchiveEntry entry, StorageFolder folder) { try { using (Stream entryStream = entry.Open()) { byte[] buffer = new byte[entry.Length]; entryStream.Read(buffer, 0, buffer.Length); string filePath = entry.FullName.Replace('/', '\\'); StorageFile uncompressedFile = await folder.CreateFileAsync(filePath, CreationCollisionOption.ReplaceExisting); using (var uncompressedFileStream = await uncompressedFile.OpenAsync(FileAccessMode.ReadWrite)) { using (Stream outstream = uncompressedFileStream.AsStreamForWrite()) { outstream.Write(buffer, 0, buffer.Length); outstream.Flush(); } } } } catch (Exception ex) { DownloadErrors.Add(ex.ToString()); RaisePropertyChanged("HasDownloadErrors"); } }
private static void WriteProject(XProject project, ZipArchiveEntry projectEntry, IFileSystem fileIO, IJsonSerializer serializer) { using (var jsonStream = projectEntry.Open()) { fileIO.WriteUtf8Text(jsonStream, serializer.Serialize(project)); } }
private async Task Extract(ZipArchiveEntry entry, string basePath) { var fn = Path.Combine(basePath, entry.FullName); var dir = Path.GetDirectoryName(fn); if (dir == null) { throw new UpdateException("patch path is not valid."); } try { // ensure create directory Directory.CreateDirectory(dir); // remove previous file if (File.Exists(fn)) { File.Delete(fn); } using (var fstream = File.Create(fn)) { await entry.Open().CopyToAsync(fstream); } // complete. return; } catch (Exception ex) { var resp = MessageBox.Show( "Could not extract file: " + entry.FullName + Environment.NewLine + ex.Message + Environment.NewLine + "Would you like to retry?", "Krile Automatic Update", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error); if (resp == DialogResult.Cancel) { // cancelled. throw new UpdateException("Failed to apply patch."); } } // failed, retrying... await Extract(entry, basePath); }
public void EnqueueZipArchiveEntry(IProjectEntry projEntry, string zipFileName, ZipArchiveEntry entry, Action onComplete) { var pathInArchive = entry.FullName.Replace('/', '\\'); var fileName = Path.Combine(zipFileName, pathInArchive); var severity = JToolsPackage.Instance != null ? JToolsPackage.Instance.OptionsPage.IndentationInconsistencySeverity : Severity.Ignore; EnqueWorker(() => { try { using (var stream = entry.Open()) { _parser.ParseFile(projEntry, fileName, stream, severity); return; } } catch (IOException ex) { Debug.Fail(ex.Message); } catch (InvalidDataException ex) { Debug.Fail(ex.Message); } finally { onComplete(); } IJProjectEntry pyEntry = projEntry as IJProjectEntry; if (pyEntry != null) { // failed to parse, keep the UpdateTree calls balanced pyEntry.UpdateTree(null, null); } }); }
public void EnqueueZipArchiveEntry(IProjectEntry projEntry, string zipFileName, ZipArchiveEntry entry, Action onComplete) { var pathInArchive = entry.FullName.Replace('/', '\\'); var fileName = Path.Combine(zipFileName, pathInArchive); EnqueWorker(() => { try { using (var stream = entry.Open()) { ParseFile( projEntry, new Dictionary<int, CodeInfo> { { 0, new StreamCodeInfo(0, stream) } } ); return; } } catch (IOException ex) { Debug.Fail(ex.Message); } catch (InvalidDataException ex) { Debug.Fail(ex.Message); } finally { onComplete(); } IPythonProjectEntry pyEntry = projEntry as IPythonProjectEntry; if (pyEntry != null) { // failed to parse, keep the UpdateTree calls balanced pyEntry.UpdateTree(null, null); } }); }
/// <summary> /// Reads AppManifest from zip entry. Supports both 8.1 and pre 8.1 appx manifest format and UWP 10 manifest format. /// Patched version of code responsible for reading manifest from ZipEntry. Patch adds support for UWP 10 manifest. /// </summary> /// <param name="fromBundle"></param> /// <param name="entry"></param> /// <returns></returns> private static IAppManifestInfo ReadAppManifestInfoFromEntry(bool fromBundle, ZipArchiveEntry entry) { var ns = string.Empty; using (var stream = entry.Open()) { var root = XDocument.Load(stream).Root; if (root != null) { ns = root.GetDefaultNamespace().NamespaceName; } } if (ns.Equals(Windows10ManifestNamespace)) { using (var stream = entry.Open()) { return Package10.LoadFromStream(stream, fromBundle); } } using (var stream = entry.Open()) { return Package.LoadFromStream(stream, fromBundle); } }
/// <summary> /// Reading from an excel template , entering data in it and write back to a file /// </summary> /// <param name="filename"></param> /// <returns></returns> public async Task <StorageFile> WriteToFile(string filename, StorageFile targetfile) { //StorageFile targetfile = null; try { var exceltemplatestream = Assembly.Load(new AssemblyName("ExcelManager")).GetManifestResourceStream(@"ExcelManager.Resources.ExcelTemplateFile.xlsx"); System.IO.Compression.ZipArchive zipArchiveExcelTemplate = new System.IO.Compression.ZipArchive(exceltemplatestream); //Data Builder Code ExcelXmlBuilder excelbuilder = new ExcelXmlBuilder(); XmlDocument xstyleDoc = null; string sheet2string = "", sheet1string; List <Cell> sheetcells = new List <Cell>(ExcelSheet.Cells); var styles = zipArchiveExcelTemplate.Entries.FirstOrDefault(x => x.FullName == "xl/styles.xml"); var styledata = GetByteArrayFromStream(styles.Open()); xstyleDoc = GetStyleXDoc(styledata); sheet1string = excelbuilder.BuildSheetXML(ExcelSheet, ref xstyleDoc); if (ExcelSheet2 != null && ExcelSheet2.Cells != null && ExcelSheet2.Cells.Count > 0) { sheet2string = excelbuilder.BuildSheetXML(ExcelSheet2, ref xstyleDoc, ExcelSheet.Cells.Count); sheetcells.AddRange(ExcelSheet2.Cells); } using (var zipStream = await targetfile.OpenStreamForWriteAsync()) { using (System.IO.Compression.ZipArchive newzip = new System.IO.Compression.ZipArchive(zipStream, ZipArchiveMode.Create)) { foreach (var file in zipArchiveExcelTemplate.Entries) { System.IO.Compression.ZipArchiveEntry entry = newzip.CreateEntry(file.FullName); using (Stream ZipFile = entry.Open()) { byte[] data = null; if (file.FullName == "xl/sharedStrings.xml") { data = Encoding.UTF8.GetBytes(excelbuilder.BuildSharedXML(sheetcells)); ZipFile.Write(data, 0, data.Length); } else if (file.FullName == "xl/worksheets/sheet1.xml") { data = Encoding.UTF8.GetBytes(sheet1string); ZipFile.Write(data, 0, data.Length); } else if (file.FullName == "xl/worksheets/sheet2.xml" && sheet2string != "") { data = Encoding.UTF8.GetBytes(sheet2string); ZipFile.Write(data, 0, data.Length); } else if (file.FullName == "xl/styles.xml") { if (xstyleDoc != null) { data = Encoding.UTF8.GetBytes(xstyleDoc.GetXml().Replace("xmlns=\"\"", "")); ZipFile.Write(data, 0, data.Length); } } else { data = GetByteArrayFromStream(file.Open()); ZipFile.Write(data, 0, data.Length); } } } } } } catch { throw; } return(targetfile); }
private void LoadWorkbookXmlRels(ZipArchiveEntry entry) { XDocument document = XDocument.Load(entry.Open()); XElement root = document.Root; XNamespace ns = NS_PR; foreach (XElement element in root.Elements(ns + "Relationship")) { XAttribute id = element.Attribute("Id"); XAttribute type = element.Attribute("Type"); XAttribute target = element.Attribute("Target"); if (type.Value != NS_ORW) continue; Sheet sheet = (from s in this.sheets where s.Id == id.Value select s).FirstOrDefault(); // Get sheet file path Match match = Regex.Match(target.Value, @"worksheets/(.+)"); string path = @"xl/worksheets/" + match.Groups[1].Value; sheet.Path = path.ToLower(); } }
private static XProject ReadProject(ZipArchiveEntry projectEntry, IFileSystem fileIO, IJsonSerializer serializer) { using (var entryStream = projectEntry.Open()) { return serializer.Deserialize<XProject>(fileIO.ReadUtf8Text(entryStream)); } }
public IO.Stream Open() { return(_entry.Open()); }
public Stream Open(ZipArchiveEntry zipArchiveEntry, FileMode streamFileMode, FileAccess streamFileAccess) { bool canRead = true; bool canWrite = true; switch (_packageFileAccess) { case FileAccess.Read: switch (streamFileAccess) { case FileAccess.Read: canRead = true; canWrite = false; break; case FileAccess.Write: canRead = false; canWrite = false; break; case FileAccess.ReadWrite: canRead = true; canWrite = false; break; } break; case FileAccess.Write: switch (streamFileAccess) { case FileAccess.Read: canRead = false; canWrite = false; break; case FileAccess.Write: canRead = false; canWrite = true; break; case FileAccess.ReadWrite: canRead = false; canWrite = true; break; } break; case FileAccess.ReadWrite: switch (streamFileAccess) { case FileAccess.Read: canRead = true; canWrite = false; break; case FileAccess.Write: canRead = false; canWrite = true; break; case FileAccess.ReadWrite: canRead = true; canWrite = true; break; } break; } Stream ns = zipArchiveEntry.Open(); return new ZipWrappingStream(zipArchiveEntry, ns, _packageFileMode, _packageFileAccess, canRead, canWrite); }
/// <summary> /// Reads a ZipArchive entry as the routes CSV and extracts the route colors. /// </summary> private static List<GoogleRoute> ParseRouteCSV(ZipArchiveEntry entry) { var routes = new List<GoogleRoute>(); using (var reader = new StreamReader(entry.Open())) { // Ignore the format line reader.ReadLine(); while (!reader.EndOfStream) { var parts = reader.ReadLine().Split(','); // Ignore all routes which aren't part of CTS and thus don't have any real-time data. if (parts[0].Contains("ATS") || parts[0].Contains("PC") || parts[0].Contains("LBL")) { continue; } routes.Add(new GoogleRoute(parts)); } } if(!routes.Any(x => x.Name == "C1R")) { routes.Add(new GoogleRoute("C1R", "9F8E7D", "http://www.corvallisoregon.gov/index.aspx?page=1774")); } return routes; }
public static StockDetails Load(ZipArchiveEntry ze) { string symbol = Path.GetFileNameWithoutExtension(ze.Name); string ext = Path.GetExtension(ze.Name); if (ext.Equals(".csv", StringComparison.InvariantCultureIgnoreCase)) { return LoadFromTextStream(symbol, ze.Open()); } else if (ext.Equals(".tic", StringComparison.InvariantCultureIgnoreCase)) { return LoadFromBinaryStream(symbol, ze.Open()); } else { throw new ApplicationException("Unsupported file extension"); } }
private static List<GoogleRouteSchedule> ParseScheduleCSV(ZipArchiveEntry entry) { using (var reader = new StreamReader(entry.Open())) { // skip format line reader.ReadLine(); var lines = ReadLines(reader).ToList(); var flatSchedule = lines.Select(line => line.Split(',')) .Where(line => !string.IsNullOrWhiteSpace(line[1])) .Select(line => new { route = s_routePattern.Match(line[0]).Groups[1].Value, stop = line[3], order = line[4], days = DaysOfWeekUtils.GetDaysOfWeek(line[0]), time = ToTimeSpan(line[1].Replace("\"", string.Empty)) }); // Time to turn some totally flat data into totally structured data. var routeDayStopSchedules = flatSchedule.GroupBy(line => new { route = line.route, stop = line.stop, // stops can appear more than once in a route (particularly at the very beginning and very end) // we want to separate each section of the schedule in which the same stop appears. order = line.order, days = line.days }) .Select(grouping => grouping.Aggregate(new List<TimeSpan>(), (times, time) => { times.Add(time.time); return times; }, times => new { route = grouping.Key.route, days = grouping.Key.days, stopSchedules = new GoogleStopSchedule { Name = grouping.Key.stop, Times = times.Distinct().OrderBy(time => time).ToList() } })); var routeDaySchedules = routeDayStopSchedules .GroupBy(line => new { route = line.route, days = line.days }) .Select(grouping => grouping.Aggregate(new { route = grouping.Key.route, daySchedule = new GoogleDaySchedule { Days = grouping.Key.days, StopSchedules = new List<GoogleStopSchedule>() } }, (result, line) => { result.daySchedule.StopSchedules.Add(line.stopSchedules); return result; })); // the aristocrats! IList<GoogleRouteSchedule> routeSchedules = routeDaySchedules .GroupBy(line => line.route) .Select(grouping => grouping.Aggregate(new GoogleRouteSchedule { RouteNo = grouping.Key, Days = new List<GoogleDaySchedule>() }, (result, line) => { result.Days.Add(line.daySchedule); return result; })).ToList(); if(!routeSchedules.Any(x => x.RouteNo == "C1R")) { routeSchedules.Add(GetC1RSchedule()); } return routeSchedules.ToList(); } }
public ZipPackageFile(ZipArchiveEntry part) : this(part.FullName.Replace('/', '\\'), part.Open().ToStreamFactory()) { }
private static void CopyStream(ZipArchiveEntry entry, MemoryStream ms) { var buffer = new byte[16*1024 - 1]; int read; var stream = entry.Open(); while ((read = stream.Read(buffer, 0, buffer.Length)) > 0) { ms.Write(buffer, 0, read); } }
private void LoadWorkbookXml(ZipArchiveEntry entry) { XDocument document = XDocument.Load(entry.Open()); XElement root = document.Root; XNamespace ns = NS_MAIN; XNamespace r = NS_OR; foreach (XElement element in root.Element(ns + "sheets").Elements()) { XAttribute id = element.Attribute(r + "id"); XAttribute name = element.Attribute("name"); XAttribute state = element.Attribute("state"); bool hidden = false; if (state != null && state.Value == "hidden") hidden = true; Sheet sheet = new Sheet(name.Value, id.Value, hidden); sheet.Workbook = this; this.sheets.Add(sheet); } }
private void Load(ZipArchiveEntry entry) { XDocument document = XDocument.Load(entry.Open()); XElement root = document.Root; XNamespace ns = NS_MAIN; // Find hidden columns List<int> hiddenColumns = GetHiddenColumns(root, ns); // Loop throgh rows XElement sheetData = root.Element(ns + "sheetData"); foreach (XElement eRow in sheetData.Elements(ns + "row")) { // Skip empty rows if (eRow.Descendants(ns + "v").Count() == 0) continue; // Set row properties XAttribute attr1 = eRow.Attribute("r"); XAttribute attr2 = eRow.Attribute("hidden"); int index = Convert.ToInt16(attr1.Value); bool hidden = (attr2 != null && attr2.Value == "1") ? true : false; Row row = new Row(index, hidden); // Loop through cells on row foreach (XElement eCell in eRow.Elements(ns + "c")) { // Skip empty cells XElement xValue = eCell.Element(ns + "v"); if (xValue == null) continue; // Get cell position string position = eCell.Attribute("r").Value; Match match = Regex.Match(position, @"([A-Z]+)(\d+)"); string letters = match.Groups[1].Value; string numbers = match.Groups[2].Value; int columnIndex = GetColumnIndex(letters); int rowIndex = Convert.ToInt16(numbers); // Get cell content int number = Convert.ToInt16(xValue.Value); string sharedString = string.Empty; this.workbook.SharedStrings.TryGetValue(number, out sharedString); // Make column Column column = GetColumn(columnIndex); column.Hidden = (hiddenColumns.Contains(columnIndex)) ? true : false; // Make cell Cell cell = new Cell(sharedString); cell.Column = column; cell.Row = row; // Add cell to row and column row.AddCell(cell); column.AddCell(cell); // Add rows and column to sheet this.AddRow(row); this.AddColumn(column); /* We add rows and columns multiple times here and let * the add methods filter out existing ones. */ } } }
private void LoadSharedStrings(ZipArchiveEntry entry) { // The xl/sharedStrings.xml file will be missing in a completely blank file if (entry == null) return; XDocument document = XDocument.Load(entry.Open()); XElement root = document.Root; XNamespace ns = NS_MAIN; int count = 0; foreach (XElement si in root.Elements(ns + "si")) { IEnumerable<XElement> ts = si.Descendants(ns + "t"); string sum = string.Empty; foreach (XElement t in ts) sum += t.Value; this.sharedStrings.Add(count, sum); count++; } }
private static Stream GetSizeVerifiedFileStream(ZipArchiveEntry zipEntry, int maxSize) { Debug.Assert(zipEntry != null); // claimedLength = What the zip file header claims is the uncompressed length // let's not be too trusting when it comes to that claim. var claimedLength = zipEntry.Length; if (claimedLength < 0) { throw new InvalidDataException("The zip entry size is invalid."); } if (claimedLength > maxSize) { throw new InvalidDataException("The zip entry is larger than the allowed size."); } // Read at most the claimed number of bytes from the array. byte[] safeBytes; int bytesRead; using (Stream unsafeStream = zipEntry.Open()) { safeBytes = new byte[claimedLength]; bytesRead = unsafeStream.Read(safeBytes, 0, (int)claimedLength); if (bytesRead != claimedLength) { throw new InvalidDataException("The zip entry's claimed decompressed size is incorrect."); } } return new MemoryStream(safeBytes, 0, bytesRead); }