void _checkDBFilePath(string path, string _dbfileName) { //创建目录与文件 第1层,第2层的文件保存方式 string rootDir = path; if (!Directory.Exists(rootDir)) { FileEntryInfo.CheckDirectory_STC(rootDir); } string dbname = Path.Combine(rootDir, _dbfileName); //创建根目录 下保存的文件对象位置 bool fileExist = File.Exists(dbname);//存在文件就直接打开,否则创建 //conn = new SQLiteConnection(dbname, fileExist ? SQLiteOpenFlags.ReadWrite : (SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.Create)); if (fileExist) { //检测是否为压缩文件 using (FileStream fst = new FileStream(dbname, FileMode.Open, FileAccess.Read)) { fileExist = SharpCompress.Archive.Zip.ZipArchive.IsZipFile(fst);//非压缩文件 //压缩文件系统 没必要一直处于打开状态吧 用完销毁 } } if (!fileExist) { using (SharpCompress.Archive.Zip.ZipArchive con = SharpCompress.Archive.Zip.ZipArchive.Create()) { using (FileStream fst = new FileStream(dbname, FileMode.Create, FileAccess.Write)) { con.SaveTo(fst, new SharpCompress.Common.CompressionInfo());//保存完毕 } } } }
// public void Close() { if (this.ZipTarget != null && !this.ZipTarget.IsDisposed) { this.ZipTarget.Dispose(); } this.ZipTarget = null; }
private void CreateZip(Stream stream, ZipArchiveMode mode) { try { if (mode != ZipArchiveMode.Read && mode != ZipArchiveMode.Create && mode != ZipArchiveMode.Update) { throw new ArgumentOutOfRangeException("mode"); } // If the mode parameter is set to Read, the stream must support reading. if (mode == ZipArchiveMode.Read && !stream.CanRead) { throw new ArgumentException("Stream must support reading for Read archive mode"); } // If the mode parameter is set to Create, the stream must support writing. if (mode == ZipArchiveMode.Create && !stream.CanWrite) { throw new ArgumentException("Stream must support writing for Create archive mode"); } // If the mode parameter is set to Update, the stream must support reading, writing, and seeking. if (mode == ZipArchiveMode.Update && (!stream.CanRead || !stream.CanWrite || !stream.CanSeek)) { throw new ArgumentException("Stream must support reading, writing and seeking for Update archive mode"); } try { zipFile = mode != ZipArchiveMode.Create && stream.Length != 0 ? SharpCompress.Archive.Zip.ZipArchive.Open(stream) : SharpCompress.Archive.Zip.ZipArchive.Create(); } catch (Exception e) { throw new InvalidDataException("The contents of the stream are not in the zip archive format.", e); } entries = new Dictionary <string, ZipArchiveEntry>(); if (Mode != ZipArchiveMode.Create) { foreach (var entry in zipFile.Entries) { var zipEntry = new ZipArchiveEntry(this, entry); entries[entry.Key] = zipEntry; } } } catch { if (!leaveStreamOpen) { stream.Dispose(); } throw; } }
/// <summary> /// 根据文件名,取得存储数据的zip /// </summary> /// <param name="strFileName">相对与table根目录的路径</param> /// <returns></returns> DiskZip_ConnectInfo _checkFileDataZip(string strFileName) { string zipDataPath = _getFileDataZipPath(strFileName); bool fileExist = File.Exists(zipDataPath); if (!fileExist) { FileEntryInfo.CheckDirectory_STC(Path.GetDirectoryName(zipDataPath)); using (SharpCompress.Archive.Zip.ZipArchive con = SharpCompress.Archive.Zip.ZipArchive.Create()) { using (FileStream fst = new FileStream(zipDataPath, FileMode.Create, FileAccess.Write)) { con.SaveTo(fst, new SharpCompress.Common.CompressionInfo());//保存完毕 } } } if (!m_FileDataZips.ContainsKey(zipDataPath)) { //创建 if (fileExist) { //检测是否为压缩文件 using (FileStream fst = new FileStream(zipDataPath, FileMode.Open, FileAccess.Read)) { fileExist = SharpCompress.Archive.Zip.ZipArchive.IsZipFile(fst);//非压缩文件 //压缩文件系统 没必要一直处于打开状态吧 用完销毁 } } if (!fileExist) { FileEntryInfo.CheckDirectory_STC(Path.GetDirectoryName(zipDataPath)); using (SharpCompress.Archive.Zip.ZipArchive con = SharpCompress.Archive.Zip.ZipArchive.Create()) { using (FileStream fst = new FileStream(zipDataPath, FileMode.Create, FileAccess.Write)) { con.SaveTo(fst, new SharpCompress.Common.CompressionInfo());//保存完毕 } } } DiskZip_ConnectInfo dc = new DiskZip_ConnectInfo(); dc.ConnString = zipDataPath; m_FileDataZips.Add(zipDataPath, dc); } return(m_FileDataZips[zipDataPath]); }
/// <summary> /// 检测压缩文件是否存在,如果不存在则创建文件并加入表中,返回连接.z文件对象 /// </summary> /// <param name="tableName"></param> /// <returns></returns> internal DiskReadZip_ConnectInfo CheckConnection(string tableName) { if (!m_Conns.ContainsKey(tableName)) { //创建 string dbname = _GetTableFileFullPath(tableName); bool fileExist = File.Exists(dbname);//存在文件就直接打开,否则创建 //conn = new SQLiteConnection(dbname, fileExist ? SQLiteOpenFlags.ReadWrite : (SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.Create)); if (fileExist) { //检测是否为压缩文件 using (FileStream fst = new FileStream(dbname, FileMode.Open, FileAccess.Read)) { fileExist = SharpCompress.Archive.Zip.ZipArchive.IsZipFile(fst);//非压缩文件 //压缩文件系统 没必要一直处于打开状态吧 用完销毁 } } if (!fileExist) { using (SharpCompress.Archive.Zip.ZipArchive con = SharpCompress.Archive.Zip.ZipArchive.Create()) { using (FileStream fst = new FileStream(dbname, FileMode.Create, FileAccess.Write)) { con.SaveTo(fst, new SharpCompress.Common.CompressionInfo());//保存完毕 } } } // //FileStream fs = new FileStream(dbname, FileMode.Open, FileAccess.ReadWrite); //conn = SharpCompress.Archive.Zip.ZipArchive.Open(fs); DiskReadZip_ConnectInfo ci = new DiskReadZip_ConnectInfo(); ci.ConnString = dbname; ci.Name = tableName; m_Conns.Add(tableName, ci);//添加在数据表中 // } //打开,用的时候再打开吧 //m_Conns[tableName].Open(); return(m_Conns[tableName]); }
private void CreateZip(Stream stream, ZipArchiveMode mode) { if (mode != ZipArchiveMode.Read && mode != ZipArchiveMode.Create && mode != ZipArchiveMode.Update) throw new ArgumentOutOfRangeException("mode"); // If the mode parameter is set to Read, the stream must support reading. if (mode == ZipArchiveMode.Read && !stream.CanRead) throw new ArgumentException("Stream must support reading for Read archive mode"); // If the mode parameter is set to Create, the stream must support writing. if (mode == ZipArchiveMode.Create && !stream.CanWrite) throw new ArgumentException("Stream must support writing for Create archive mode"); // If the mode parameter is set to Update, the stream must support reading, writing, and seeking. if (mode == ZipArchiveMode.Update && (!stream.CanRead || !stream.CanWrite || !stream.CanSeek)) throw new ArgumentException("Stream must support reading, writing and seeking for Update archive mode"); try { zipFile = mode != ZipArchiveMode.Create && stream.Length != 0 ? SharpCompress.Archive.Zip.ZipArchive.Open(stream) : SharpCompress.Archive.Zip.ZipArchive.Create(); } catch (Exception e) { throw new InvalidDataException("The contents of the stream are not in the zip archive format.", e); } entries = new Dictionary<string, ZipArchiveEntry>(); if (Mode != ZipArchiveMode.Create) { foreach (var entry in zipFile.Entries) { var zipEntry = new ZipArchiveEntry(this, entry); entries[entry.Key] = zipEntry; } } }
public void Open() { if (this.ZipTarget != null && !this.ZipTarget.IsDisposed) { return; } FileStream fs = new FileStream(ConnString, FileMode.Open, FileAccess.ReadWrite); this.ZipTarget = SharpCompress.Archive.Zip.ZipArchive.Open(fs); }
private void CreateZip(ZipArchiveMode mode) { try { if (mode != ZipArchiveMode.Read && mode != ZipArchiveMode.Create && mode != ZipArchiveMode.Update) throw new ArgumentOutOfRangeException("mode"); // If the mode parameter is set to Read, the stream must support reading. if (mode == ZipArchiveMode.Read && !stream.CanRead) throw new ArgumentException("Stream must support reading for Read archive mode"); // If the mode parameter is set to Create, the stream must support writing. if (mode == ZipArchiveMode.Create && !stream.CanWrite) throw new ArgumentException("Stream must support writing for Create archive mode"); // If the mode parameter is set to Update, the stream must support reading, writing, and seeking. if (mode == ZipArchiveMode.Update && (!stream.CanRead || !stream.CanWrite || !stream.CanSeek)) throw new ArgumentException("Stream must support reading, writing and seeking for Update archive mode"); // If the stream is not seekable, then buffer it into memory (same behavior as .NET). if (mode == ZipArchiveMode.Read && !stream.CanSeek) { var memoryStream = new MemoryStream(); stream.CopyTo(memoryStream); if (!leaveStreamOpen) stream.Dispose(); this.stream = memoryStream; } try { zipFile = mode != ZipArchiveMode.Create && stream.Length != 0 ? SharpCompress.Archive.Zip.ZipArchive.Open(stream) : SharpCompress.Archive.Zip.ZipArchive.Create(); } catch (Exception e) { throw new InvalidDataException("The contents of the stream are not in the zip archive format.", e); } entries = new List<ZipArchiveEntry>(); if (Mode != ZipArchiveMode.Create) { foreach (var entry in zipFile.Entries) { var zipEntry = new ZipArchiveEntry(this, entry); entries.Add(zipEntry); } } } catch { if (!leaveStreamOpen) stream.Dispose(); throw; } }
private void CreateZip(ZipArchiveMode mode) { try { if (mode != ZipArchiveMode.Read && mode != ZipArchiveMode.Create && mode != ZipArchiveMode.Update) { throw new ArgumentOutOfRangeException("mode"); } // If the mode parameter is set to Read, the stream must support reading. if (mode == ZipArchiveMode.Read && !stream.CanRead) { throw new ArgumentException("Stream must support reading for Read archive mode"); } // If the mode parameter is set to Create, the stream must support writing. if (mode == ZipArchiveMode.Create && !stream.CanWrite) { throw new ArgumentException("Stream must support writing for Create archive mode"); } // If the mode parameter is set to Update, the stream must support reading, writing, and seeking. if (mode == ZipArchiveMode.Update && (!stream.CanRead || !stream.CanWrite || !stream.CanSeek)) { throw new ArgumentException("Stream must support reading, writing and seeking for Update archive mode"); } // If the stream is not seekable, then buffer it into memory (same behavior as .NET). if (mode == ZipArchiveMode.Read && !stream.CanSeek) { var memoryStream = new MemoryStream(); stream.CopyTo(memoryStream); if (!leaveStreamOpen) { stream.Dispose(); } this.stream = memoryStream; } try { zipFile = mode != ZipArchiveMode.Create && stream.Length != 0 ? SharpCompress.Archive.Zip.ZipArchive.Open(stream) : SharpCompress.Archive.Zip.ZipArchive.Create(); } catch (Exception e) { throw new InvalidDataException("The contents of the stream are not in the zip archive format.", e); } entries = new List <ZipArchiveEntry>(); if (Mode != ZipArchiveMode.Create) { foreach (var entry in zipFile.Entries) { var zipEntry = new ZipArchiveEntry(this, entry); entries.Add(zipEntry); } } } catch { if (!leaveStreamOpen) { stream.Dispose(); } throw; } }