Beispiel #1
0
        public Stream OpenWrite()
        {
            try
            {
                if (OsmFile.FullName == string.Empty)
                {
                    throw new ArgumentException("File name is null");
                }
                else if (!OsmFile.Exists)
                {
                    OsmFile.Create();
                }

                if (Zipped == true)
                {
                    if (ZipArchive != null)
                    {
                        ZipArchive.Dispose();
                        ZipArchive = null;
                    }

                    ZipArchive = new ZipArchive(OsmFile.OpenRead(), ZipArchiveMode.Update);
                    ZipArchiveEntry osmFileEntry = ZipArchive.CreateEntry(string.Format("{0}.osm", OsmFile.Name.Substring(0, OsmFile.Name.Length - OsmFile.Extension.Length)));
                    return(osmFileEntry.Open());
                }
                else
                {
                    return(OsmFile.OpenWrite());
                }
            } catch (Exception ex)
            {
                LogHelper.LogException(ex);
                return(null);
            }
        }
Beispiel #2
0
        public Stream OpenRead()
        {
            try
            {
                if (!OsmFile.Exists)
                {
                    throw new FileNotFoundException(m_fileInfo.Name);
                }

                if (Zipped == true)
                {
                    if (ZipArchive != null)
                    {
                        ZipArchive.Dispose();
                        ZipArchive = null;
                    }

                    ZipArchive = ZipFile.Open(OsmFile.FullName, ZipArchiveMode.Read);
                    ZipArchiveEntry entry = ZipArchive.GetEntry(OsmFile.Name.Substring(0, OsmFile.Name.Length - OsmFile.Extension.Length));
                    return(entry.Open());
                }
                else
                {
                    return(OsmFile.OpenRead());
                }
            } catch (Exception ex)
            {
                LogHelper.LogException(ex);
                return(null);
            }
        }
Beispiel #3
0
        public void WriteAllText(string input)
        {
            try
            {
                if (OsmFile.FullName == string.Empty)
                {
                    throw new ArgumentException("File name is null");
                }
                else if (Zipped == true)
                {
                    if (ZipArchive != null)
                    {
                        ZipArchive.Dispose();
                        ZipArchive = null;
                    }

                    ZipArchive = new ZipArchive(OsmFile.OpenRead(), ZipArchiveMode.Update);
                    ZipArchiveEntry osmFileEntry = null;
                    string          entryName    = string.Format("{0}.osm", OsmFile.Name.Substring(0, OsmFile.Name.Length - OsmFile.Extension.Length));
                    try
                    {
                        osmFileEntry = ZipArchive.GetEntry(entryName);
                    }
                    catch (Exception ex)
                    {
                    }
                    if (osmFileEntry == null)
                    {
                        osmFileEntry = ZipArchive.CreateEntry(entryName);
                    }

                    using (Stream stream = osmFileEntry.Open())
                    {
                        using (StreamWriter sw = new StreamWriter(stream))
                        {
                            sw.Write(input);
                        }
                    }
                }
                else
                {
                    File.WriteAllText(OsmFile.FullName, input);
                }
            } catch (Exception ex)
            {
                LogHelper.LogException(ex);
            }
        }
Beispiel #4
0
        public byte[] ReadAllBytes()
        {
            try
            {
                if (!OsmFile.Exists)
                {
                    throw new FileNotFoundException(OsmFile.FullName);
                }
                else if (Zipped == true)
                {
                    if (ZipArchive != null)
                    {
                        ZipArchive.Dispose();
                        ZipArchive = null;
                    }

                    ZipArchive = new ZipArchive(OsmFile.OpenRead(), ZipArchiveMode.Update);
                    ZipArchiveEntry osmFileEntry = ZipArchive.CreateEntry(string.Format("{0}.osm", OsmFile.Name.Substring(0, OsmFile.Name.Length - OsmFile.Extension.Length)));

                    using (Stream stream = osmFileEntry.Open())
                    {
                        using (MemoryStream memStream = new MemoryStream())
                        {
                            byte[] buffer = new byte[16 * 1024];
                            int    read;
                            while ((read = stream.Read(buffer, 0, buffer.Length)) > 0)
                            {
                                memStream.Write(buffer, 0, read);
                            }
                            return(memStream.ToArray());
                        }
                    }
                }
                else
                {
                    return(File.ReadAllBytes(OsmFile.FullName));
                }
            } catch (Exception ex)
            {
                LogHelper.LogException(ex);
                return(null);
            }
        }
Beispiel #5
0
        public string ReadAllText()
        {
            try
            {
                if (!OsmFile.Exists)
                {
                    throw new FileNotFoundException(OsmFile.FullName);
                }
                else if (Zipped == true)
                {
                    if (ZipArchive != null)
                    {
                        ZipArchive.Dispose();
                        ZipArchive = null;
                    }

                    ZipArchive = new ZipArchive(OsmFile.OpenRead(), ZipArchiveMode.Update);
                    ZipArchiveEntry osmFileEntry = ZipArchive.CreateEntry(string.Format("{0}.osm", OsmFile.Name.Substring(0, OsmFile.Name.Length - OsmFile.Extension.Length)));

                    using (Stream stream = osmFileEntry.Open())
                    {
                        using (StreamReader sr = new StreamReader(stream))
                        {
                            return(sr.ReadToEnd());
                        }
                    }
                }
                else
                {
                    return(File.ReadAllText(OsmFile.FullName));
                }
            } catch (Exception ex)
            {
                LogHelper.LogException(ex);
                return("");
            }
        }