public Stream OpenFileAsStream(string strFileName)
        {
            strFileName = DiskReadZip_FilePacker._getFileLegalLowerDir(strFileName);
            if (string.IsNullOrEmpty(strFileName))
            {
                throw new ArgumentNullException(strFileName);
            }
            this.m_Conn = this.m_Packer.CheckConnection(this.Name);
            DiskReadZip_ConnectInfo conn = this.m_Conn;

            conn.Open();
            SharpCompress.Archive.Zip.ZipArchiveEntry findFile = null;
            foreach (var v in conn.ZipTarget.Entries)
            {
                if (v.IsDirectory)
                {
                    continue;
                }
                if (string.Compare(v.Key, strFileName, true) == 0)
                {
                    findFile = v;
                    break;
                }
            }
            if (findFile != null)
            {
                return(conn.GetUnCompressStream(findFile));
            }
            return(null);
        }
        public bool FileExists(string strFileName)
        {
            strFileName = DiskReadZip_FilePacker._getFileLegalLowerDir(strFileName);
            if (string.IsNullOrEmpty(strFileName))
            {
                return(false);
            }
            this.m_Conn = this.m_Packer.CheckConnection(this.Name);
            DiskReadZip_ConnectInfo conn = this.m_Conn;

            conn.Open();
            SharpCompress.Archive.Zip.ZipArchiveEntry findFile = null;
            foreach (var v in conn.ZipTarget.Entries)
            {
                if (v.IsDirectory)
                {
                    continue;
                }
                if (string.Compare(v.Key, strFileName, true) == 0)
                {
                    findFile = v;
                    break;
                }
            }
            if (findFile != null)
            {
                return(true);
            }
            return(false);
        }
        /// <summary>
        /// 返回规范化的目录名称 不以'/'开头和结尾
        /// </summary>
        /// <param name="dirs"></param>
        /// <returns></returns>
        public int GetDirs(out List <string> dirs)
        {
            dirs        = new List <string>();
            this.m_Conn = this.m_Packer.CheckConnection(this.Name);
            DiskReadZip_ConnectInfo conn = this.m_Conn;

            conn.Open();
            string fileInDir = "";
            string strFile;

            foreach (var ze in conn.ZipTarget.Entries)
            {
                if (ze.Key != "/")  //最顶层就不要了
                {
                    fileInDir = DiskReadZip_FilePacker._getFileLegalLowerDir(ze.Key);
                    fileInDir = GetFirstDir(fileInDir, out strFile);
                    if (!dirs.Contains(fileInDir))
                    {
                        dirs.Add(fileInDir);
                    }
                }
            }

            return(dirs.Count);
        }
        public void DelFile(string strFileName)
        {
            strFileName = DiskReadZip_FilePacker._getFileLegalLowerDir(strFileName);
            if (string.IsNullOrEmpty(strFileName))
            {
                return;
            }
            //压缩包文件存储格式
            //目录 "a/b/c/"
            //文件 "a/test.txt"
            this.m_Conn = this.m_Packer.CheckConnection(this.Name);
            DiskReadZip_ConnectInfo conn = this.m_Conn;

            conn.Open();
            SharpCompress.Archive.Zip.ZipArchiveEntry findFile = null;
            foreach (var v in conn.ZipTarget.Entries)
            {
                if (v.IsDirectory)
                {
                    continue;
                }
                if (string.Compare(v.Key, strFileName, true) == 0)
                {
                    findFile = v;
                    break;
                }
            }
            if (findFile != null)
            {
                conn.ZipTarget.RemoveEntry(findFile);
                findFile.Close();
            }
        }
        //
        /// <summary>
        /// 打开压缩包
        /// </summary>
        /// <param name="tableName"></param>
        public void BeginUpdate(string tableName)
        {
            DiskReadZip_ConnectInfo conn = CheckConnection(tableName);

            //conn.BeginTransaction();
            conn.Open();
        }
        public byte[] OpenFile(string strFileName)
        {
            strFileName = DiskReadZip_FilePacker._getFileLegalLowerDir(strFileName);
            if (string.IsNullOrEmpty(strFileName))
            {
                throw new ArgumentNullException(strFileName);
            }
            this.m_Conn = this.m_Packer.CheckConnection(this.Name);
            DiskReadZip_ConnectInfo conn = this.m_Conn;

            conn.Open();
            SharpCompress.Archive.Zip.ZipArchiveEntry findFile = null;
            foreach (var v in conn.ZipTarget.Entries)
            {
                if (v.IsDirectory)
                {
                    continue;
                }
                if (string.Compare(v.Key, strFileName, true) == 0)
                {
                    findFile = v;
                    break;
                }
            }
            byte[] buf = null;
            if (findFile != null)
            {
                using (MemoryStream ms = conn.GetUnCompressStream(findFile)) {
                    buf = ms.ToArray();
                    ms.Close();//关闭流
                }
            }
            return(buf);
        }
        public DateTime GetUpdateDate(string strFileName)
        {
            strFileName = DiskReadZip_FilePacker._getFileLegalLowerDir(strFileName);
            if (string.IsNullOrEmpty(strFileName))
            {
                throw new ArgumentNullException(strFileName);
            }
            this.m_Conn = this.m_Packer.CheckConnection(this.Name);
            DiskReadZip_ConnectInfo conn = this.m_Conn;

            conn.Open();
            SharpCompress.Archive.Zip.ZipArchiveEntry findFile = null;
            DateTime updateTime = DateTime.MinValue;

            foreach (var v in conn.ZipTarget.Entries)
            {
                if (v.IsDirectory)
                {
                    continue;
                }
                if (string.Compare(v.Key, strFileName, true) == 0)
                {
                    findFile   = v;
                    updateTime = v.LastModifiedTime ?? DateTime.MinValue;
                    break;
                }
            }

            return(updateTime);
        }
        /// <summary>
        /// dir要规范,不要带'/'结尾
        /// </summary>
        /// <param name="strDir">strDir不能为""或者"/" 或者"\\"</param>
        public void DelDir(string strDir)
        {
            strDir = DiskReadZip_FilePacker._getFileLegalLowerDir(strDir);
            if (string.IsNullOrEmpty(strDir))
            {
                return;//不能删除所有?
            }
            strDir      = strDir + "/";
            this.m_Conn = this.m_Packer.CheckConnection(this.Name);
            DiskReadZip_ConnectInfo conn = this.m_Conn;

            conn.Open();
            //目录对上的就要删除
            //修改所有的匹配目录名称
            List <SharpCompress.Archive.Zip.ZipArchiveEntry> olddirs = new List <SharpCompress.Archive.Zip.ZipArchiveEntry>();

            foreach (var ze in conn.ZipTarget.Entries)
            {
                //if (ze.IsDirectory) {
                if (ze.Key.ToLower().StartsWith(strDir))
                {
                    olddirs.Add(ze);
                }
                //}
            }

            //删除目录以及其中的文件
            for (int i = olddirs.Count - 1; i >= 0; i--)
            {
                conn.ZipTarget.RemoveEntry(olddirs[i]);
                olddirs[i].Close();
            }
        }
        public int GetFiles(string strDir, out List <string> fileNames, out int totalSize)
        {
            fileNames   = new List <string>();
            totalSize   = 0;
            strDir      = DiskReadZip_FilePacker._getFileLegalLowerDir(strDir);
            this.m_Conn = this.m_Packer.CheckConnection(this.Name);
            DiskReadZip_ConnectInfo conn = this.m_Conn;

            conn.Open();
            //所有的匹配目录名称
            List <SharpCompress.Archive.Zip.ZipArchiveEntry> filesInDir = new List <SharpCompress.Archive.Zip.ZipArchiveEntry>();

            if (string.IsNullOrEmpty(strDir))
            {
                //最顶层 则是所有的文件
                foreach (var ze in conn.ZipTarget.Entries)
                {
                    if (!ze.IsDirectory)
                    {
                        filesInDir.Add(ze);
                    }
                }
            }
            else
            {
                //非最顶层
                strDir = strDir + "/";
                //ToDo:查找里面的所有文件
                foreach (var ze in conn.ZipTarget.Entries)
                {
                    if (!ze.IsDirectory)
                    {
                        if (ze.Key.ToLower().StartsWith(strDir))
                        {
                            filesInDir.Add(ze);
                        }
                    }
                }
            }
            foreach (var v in filesInDir)
            {
                fileNames.Add(v.Key);
                totalSize += (int)v.Size;
            }
            //
            int count = filesInDir.Count;

            return(count);
        }
        /// <summary>
        /// 保存压缩包,中途不要保存
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="success">false:放弃修改,直接关闭。true:先保存到缓存,完成后覆盖到当前</param>
        public void EndUpdate(string tableName, bool success)
        {
            DiskReadZip_ConnectInfo conn = CheckConnection(tableName);

            if (success)
            {
                conn.Save();//代价是否大了点?
            }
            else
            {
                //conn.Rollback();//放弃 重新打开
                conn.Close();//放弃,关闭
            }
            //(file as IRQ_FileTable).m_trans = null;
        }
        public IFileSysPackerStrategy GetFileTable(string tableName)
        {
            if (string.IsNullOrEmpty(tableName))
            {
                throw new ArgumentNullException(tableName);
            }
            if (!IsTableExists(tableName))
            {
                return(null);
            }
            DiskReadZip_ConnectInfo m_Conn = CheckConnection(tableName);

            IFileSysPackerStrategy ret = new DiskReadZip_FileTable(this, m_Conn);

            ret.Name = tableName;
            return(ret);
        }
        public void Clean()
        {
            //清除所有
            this.m_Conn = this.m_Packer.CheckConnection(this.Name);
            DiskReadZip_ConnectInfo conn = this.m_Conn;

            conn.Open();
            List <SharpCompress.Archive.Zip.ZipArchiveEntry> allFiles = new List <SharpCompress.Archive.Zip.ZipArchiveEntry>();

            foreach (var v in conn.ZipTarget.Entries)
            {
                allFiles.Add(v);
            }
            //移除所有项目
            for (int i = allFiles.Count - 1; i >= 0; i--)
            {
                conn.ZipTarget.RemoveEntry(allFiles[i]);
                allFiles[i].Close();
            }
            allFiles.Clear();
        }
        /// <summary>
        /// 一般不使用吧,外面判断新文件名是否已经存在,不要'/'开头
        /// </summary>
        /// <param name="strFileName">相对目录路径名</param>
        /// <param name="strNewFile">相对目录路径名</param>
        public void RenameFile(string strFileName, string strNewFile)
        {
            strFileName = DiskReadZip_FilePacker._getFileLegalLowerDir(strFileName);
            strNewFile  = DiskReadZip_FilePacker._getFileLegalLowerDir(strNewFile);
            if (string.IsNullOrEmpty(strFileName) || string.IsNullOrEmpty(strNewFile))
            {
                return;                                                                       //名称不能为空
            }
            if (string.Compare(strFileName, strNewFile) == 0)
            {
                return;                                              //没有修改
            }
            this.m_Conn = this.m_Packer.CheckConnection(this.Name);
            DiskReadZip_ConnectInfo conn = this.m_Conn;

            //ToDo:
            conn.Open();
            SharpCompress.Archive.Zip.ZipArchiveEntry findfile = null;

            foreach (var ze in conn.ZipTarget.Entries)
            {
                if (ze.IsDirectory)
                {
                    continue;
                }
                if (string.Compare(ze.Key, strFileName, true) == 0)
                {
                    findfile = ze;
                    break;
                }
            }
            if (findfile != null)
            {
                //外面判断新文件是否存在
                MemoryStream ms = conn.GetUnCompressStream(findfile);
                conn.ZipTarget.AddEntry(strNewFile, ms, true, ms.Length, findfile.LastModifiedTime);
                conn.ZipTarget.RemoveEntry(findfile);
                findfile.Close();
            }
        }
        /// <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]);
        }
        public void AddFile(string strFileName, byte[] fileData, DateTime date)
        {
            strFileName = DiskReadZip_FilePacker._getFileLegalLowerDir(strFileName);
            if (string.IsNullOrEmpty(strFileName))
            {
                throw new ArgumentNullException(strFileName);
            }
            DiskReadZip_FileItemInfo fi = new DiskReadZip_FileItemInfo();
            string strFile;
            string strDir = GetFirstDir(strFileName, out strFile);

            fi.FileDir        = strDir;                 //Path.GetDirectoryName(strFileName);
            fi.FileName       = strFile;                //Path.GetFileName(strFileName);
            fi.FileLen        = fileData.Length;
            fi.FileUpdateTime = fi.DateTimeToStr(date); //.ToString("yyyy-MM-dd HH:mm");
            fi.FileData       = fileData;
            this.m_Conn       = this.m_Packer.CheckConnection(this.Name);
            DiskReadZip_ConnectInfo conn = this.m_Conn;

            //conn.Insert(fi);
            //注意重名问题,外面判断
            conn.AddFile(fi);
        }
        public void UpdateFile(string strFileName, byte[] fileData, DateTime date)
        {
            strFileName = DiskReadZip_FilePacker._getFileLegalLowerDir(strFileName);
            if (string.IsNullOrEmpty(strFileName))
            {
                throw new ArgumentNullException(strFileName);
            }
            //
            this.m_Conn = this.m_Packer.CheckConnection(this.Name);
            DiskReadZip_ConnectInfo conn = this.m_Conn;

            conn.Open();
            SharpCompress.Archive.Zip.ZipArchiveEntry findFile = null;
            foreach (var ze in conn.ZipTarget.Entries)
            {
                if (ze.IsDirectory)
                {
                    continue;
                }
                if (string.Compare(ze.Key, strFileName, true) == 0)
                {
                    findFile = ze;
                    break;
                }
            }
            //
            if (findFile != null)
            {
                conn.ZipTarget.RemoveEntry(findFile);
                findFile.Close();//删除老的
                AddFile(strFileName, fileData, date);
            }
            else
            {
                AddFile(strFileName, fileData, date);
            }
        }
        /// <summary>
        /// tableName 支持[a-F]以及[_],内部最好别带 . / \\ ?等非法字符
        /// </summary>
        /// <param name="tableName"></param>
        /// <returns></returns>
        public IFileSysPackerStrategy AddFileTable(string tableName)
        {
            if (string.IsNullOrEmpty(tableName))
            {
                throw new ArgumentNullException(tableName);
            }
            try {
                if (IsTableExists(tableName))
                {
                    throw new DiskZip_AccessPackerException(tableName + "已经存在", null);
                }

                DiskReadZip_ConnectInfo m_Conn = CheckConnection(tableName);
                IFileSysPackerStrategy  ret    = new DiskReadZip_FileTable(this, m_Conn);
                ret.Name = tableName;
                //建立SQLite_FileItemInfo结构的表名
                //m_Conn.CreateTable<SQLite_FileItemInfo>();
                return(ret);
            }
            catch (Exception ee) {
                throw new DiskZip_AccessPackerException("访问文件目录时发生错误", ee);
            }
            return(null);
        }
 /// <summary>
 /// 一般不使用
 /// </summary>
 /// <param name="tableName"></param>
 /// <param name="newName"></param>
 public void RenameFileTable(string tableName, string newName)
 {
     if (string.IsNullOrEmpty(tableName))
     {
         throw new ArgumentNullException(tableName);
     }
     if (string.IsNullOrEmpty(newName))
     {
         throw new ArgumentNullException(tableName);
     }
     if (string.Compare(tableName, newName, true) == 0)
     {
         return;                                               //没有更改,返回
     }
     try {
         if (!IsTableExists(tableName))
         {
             return;
         }
         DiskReadZip_ConnectInfo m_Conn = CheckConnection(tableName);
         m_Conn.Save();//保存一下
         if (IsTableExists(newName))
         {
             throw new DiskZip_AccessPackerException("重命名失败,newName已经存在!", null);
             DelFileTable(newName);//覆盖吗?
         }
         //移除老的
         //
         File.Copy(_GetTableFileFullPath(tableName), _GetTableFileFullPath(newName), true);
         DelFileTable(tableName);//删除老文件
         //新文件要外面自己打开
     }
     catch (Exception ee) {
         throw new DiskZip_AccessPackerException("访问文件目录时发生错误", ee);
     }
 }
 public void Clean()
 {
     //清除所有
     this.m_Conn = this.m_Packer.CheckConnection(this.Name);
     DiskReadZip_ConnectInfo conn = this.m_Conn;
     conn.Open();
     List<SharpCompress.Archive.Zip.ZipArchiveEntry> allFiles = new List<SharpCompress.Archive.Zip.ZipArchiveEntry>();
     foreach (var v in conn.ZipTarget.Entries) {
         allFiles.Add(v);
     }
     //移除所有项目
     for (int i = allFiles.Count - 1; i >= 0; i--) {
         conn.ZipTarget.RemoveEntry(allFiles[i]);
         allFiles[i].Close();
     }
     allFiles.Clear();
 }
 public void AddFile(string strFileName, byte[] fileData, DateTime date)
 {
     strFileName = DiskReadZip_FilePacker._getFileLegalLowerDir(strFileName);
     if (string.IsNullOrEmpty(strFileName)) {
         throw new ArgumentNullException(strFileName);
     }
     DiskReadZip_FileItemInfo fi = new DiskReadZip_FileItemInfo();
     string strFile;
     string strDir = GetFirstDir(strFileName, out strFile);
     fi.FileDir = strDir;//Path.GetDirectoryName(strFileName);
     fi.FileName = strFile;//Path.GetFileName(strFileName);
     fi.FileLen = fileData.Length;
     fi.FileUpdateTime = fi.DateTimeToStr(date);//.ToString("yyyy-MM-dd HH:mm");
     fi.FileData = fileData;
     this.m_Conn = this.m_Packer.CheckConnection(this.Name);
     DiskReadZip_ConnectInfo conn = this.m_Conn;
     //conn.Insert(fi);
     //注意重名问题,外面判断
     conn.AddFile(fi);
 }
 public Stream OpenFileAsStream(string strFileName)
 {
     strFileName = DiskReadZip_FilePacker._getFileLegalLowerDir(strFileName);
     if (string.IsNullOrEmpty(strFileName)) {
         throw new ArgumentNullException(strFileName);
     }
     this.m_Conn = this.m_Packer.CheckConnection(this.Name);
     DiskReadZip_ConnectInfo conn = this.m_Conn;
     conn.Open();
     SharpCompress.Archive.Zip.ZipArchiveEntry findFile = null;
     foreach (var v in conn.ZipTarget.Entries) {
         if (v.IsDirectory) continue;
         if (string.Compare(v.Key, strFileName, true) == 0) {
             findFile = v;
             break;
         }
     }
     if (findFile != null) {
         return conn.GetUnCompressStream(findFile);
     }
     return null;
 }
        public DateTime GetUpdateDate(string strFileName)
        {
            strFileName = DiskReadZip_FilePacker._getFileLegalLowerDir(strFileName);
            if (string.IsNullOrEmpty(strFileName)) {
                throw new ArgumentNullException(strFileName);
            }
            this.m_Conn = this.m_Packer.CheckConnection(this.Name);
            DiskReadZip_ConnectInfo conn = this.m_Conn;
            conn.Open();
            SharpCompress.Archive.Zip.ZipArchiveEntry findFile = null;
            DateTime updateTime = DateTime.MinValue;
            foreach (var v in conn.ZipTarget.Entries) {
                if (v.IsDirectory) continue;
                if (string.Compare(v.Key, strFileName, true) == 0) {
                    findFile = v;
                    updateTime = v.LastModifiedTime ?? DateTime.MinValue;
                    break;
                }
            }

            return updateTime;
        }
        /// <summary>
        /// 返回规范化的目录名称 不以'/'开头和结尾
        /// </summary>
        /// <param name="dirs"></param>
        /// <returns></returns>
        public int GetDirs(out List<string> dirs)
        {
            dirs = new List<string>();
            this.m_Conn = this.m_Packer.CheckConnection(this.Name);
            DiskReadZip_ConnectInfo conn = this.m_Conn;
            conn.Open();
            string fileInDir = "";
            string strFile;
            foreach (var ze in conn.ZipTarget.Entries) {
                if (ze.Key != "/") {//最顶层就不要了
                    fileInDir = DiskReadZip_FilePacker._getFileLegalLowerDir(ze.Key);
                    fileInDir = GetFirstDir(fileInDir, out strFile);
                    if (!dirs.Contains(fileInDir)) {
                        dirs.Add(fileInDir);
                    }
                }
            }

            return dirs.Count;
        }
 public void DelFile(string strFileName)
 {
     strFileName = DiskReadZip_FilePacker._getFileLegalLowerDir(strFileName);
     if (string.IsNullOrEmpty(strFileName)) {
         return;
     }
     //压缩包文件存储格式
     //目录 "a/b/c/"
     //文件 "a/test.txt"
     this.m_Conn = this.m_Packer.CheckConnection(this.Name);
     DiskReadZip_ConnectInfo conn = this.m_Conn;
     conn.Open();
     SharpCompress.Archive.Zip.ZipArchiveEntry findFile = null;
     foreach (var v in conn.ZipTarget.Entries) {
         if (v.IsDirectory) continue;
         if (string.Compare(v.Key, strFileName, true) == 0) {
             findFile = v;
             break;
         }
     }
     if (findFile != null) {
         conn.ZipTarget.RemoveEntry(findFile);
         findFile.Close();
     }
 }
 //
 internal DiskReadZip_FileTable(DiskReadZip_FilePacker packer, DiskReadZip_ConnectInfo conn)
 {
     m_Conn = conn;
     //m_buf = arg;
     m_Packer = packer;
 }
        /// <summary>
        /// dir要规范,不要=='/',不要'/'开头,不要带'/'结尾
        /// </summary>
        /// <param name="strDirName"></param>
        /// <param name="strNewDirName"></param>
        public void RenameDir(string strDirName, string strNewDirName)
        {
            strDirName    = DiskReadZip_FilePacker._getFileLegalLowerDir(strDirName);    // +"/";//规范化
            strNewDirName = DiskReadZip_FilePacker._getFileLegalLowerDir(strNewDirName); // +"/";//规范化
            if (string.IsNullOrEmpty(strDirName) || string.IsNullOrEmpty(strNewDirName))
            {
                return;//根目录名称不能改
            }
            if (string.Compare(strDirName, strNewDirName) == 0)
            {
                return;//没有改名
            }
            strDirName    = strDirName + "/";
            strNewDirName = strNewDirName + "/";

            this.m_Conn = this.m_Packer.CheckConnection(this.Name);
            DiskReadZip_ConnectInfo conn = this.m_Conn;

            conn.Open();
            //修改所有的匹配目录名称
            List <SharpCompress.Archive.Zip.ZipArchiveEntry> olddirs    = new List <SharpCompress.Archive.Zip.ZipArchiveEntry>();
            List <SharpCompress.Archive.Zip.ZipArchiveEntry> filesInDir = new List <SharpCompress.Archive.Zip.ZipArchiveEntry>();

            foreach (var ze in conn.ZipTarget.Entries)
            {
                if (ze.IsDirectory)
                {
                    if (ze.Key.ToLower().StartsWith(strDirName))
                    {
                        olddirs.Add(ze);
                    }
                }
                else
                {
                    if (ze.Key.ToLower().StartsWith(strDirName))
                    {
                        filesInDir.Add(ze);
                    }
                }
            }
            //外面判断是否已经存在新目录名
            //改名
            string oldfName = "";
            string newfName = "";

            for (int i = filesInDir.Count - 1; i >= 0; i--)
            {
                oldfName = filesInDir[i].Key;
                newfName = filesInDir[i].Key.Remove(0, strDirName.Length);
                newfName = strNewDirName + newfName;
                //
                MemoryStream ms = conn.GetUnCompressStream(filesInDir[i]);
                conn.ZipTarget.AddEntry(newfName, ms, true, ms.Length, filesInDir[i].LastModifiedTime);
                conn.ZipTarget.RemoveEntry(filesInDir[i]);
                filesInDir[i].Close();
            }
            //移除老文件夹
            for (int i = olddirs.Count - 1; i >= 0; i--)
            {
                conn.ZipTarget.RemoveEntry(olddirs[i]);
                olddirs[i].Close();
            }
        }
 public void Close()
 {
     this.m_Conn = this.m_Packer.CheckConnection(this.Name);
     this.m_Conn.Close();
 }
        /// <summary>
        /// dir要规范,不要带'/'结尾
        /// </summary>
        /// <param name="strDir">strDir不能为""或者"/" 或者"\\"</param>
        public void DelDir(string strDir)
        {
            strDir = DiskReadZip_FilePacker._getFileLegalLowerDir(strDir);
            if (string.IsNullOrEmpty(strDir)) {
                return;//不能删除所有?
            }
            strDir = strDir + "/";
            this.m_Conn = this.m_Packer.CheckConnection(this.Name);
            DiskReadZip_ConnectInfo conn = this.m_Conn;
            conn.Open();
            //目录对上的就要删除
            //修改所有的匹配目录名称
            List<SharpCompress.Archive.Zip.ZipArchiveEntry> olddirs = new List<SharpCompress.Archive.Zip.ZipArchiveEntry>();

            foreach (var ze in conn.ZipTarget.Entries) {
                //if (ze.IsDirectory) {
                if (ze.Key.ToLower().StartsWith(strDir)) {
                    olddirs.Add(ze);

                }
                //}
            }

            //删除目录以及其中的文件
            for (int i = olddirs.Count - 1; i >= 0; i--) {
                conn.ZipTarget.RemoveEntry(olddirs[i]);
                olddirs[i].Close();
            }
        }
 public void Close()
 {
     this.m_Conn = this.m_Packer.CheckConnection(this.Name);
     this.m_Conn.Close();
 }
 public bool FileExists(string strFileName)
 {
     strFileName = DiskReadZip_FilePacker._getFileLegalLowerDir(strFileName);
     if (string.IsNullOrEmpty(strFileName)) {
         return false;
     }
     this.m_Conn = this.m_Packer.CheckConnection(this.Name);
     DiskReadZip_ConnectInfo conn = this.m_Conn;
     conn.Open();
     SharpCompress.Archive.Zip.ZipArchiveEntry findFile = null;
     foreach (var v in conn.ZipTarget.Entries) {
         if (v.IsDirectory) continue;
         if (string.Compare(v.Key, strFileName, true) == 0) {
             findFile = v;
             break;
         }
     }
     if (findFile != null) {
         return true;
     }
     return false;
 }
        /// <summary>
        /// 一般不使用吧,外面判断新文件名是否已经存在,不要'/'开头
        /// </summary>
        /// <param name="strFileName">相对目录路径名</param>
        /// <param name="strNewFile">相对目录路径名</param>
        public void RenameFile(string strFileName, string strNewFile)
        {
            strFileName = DiskReadZip_FilePacker._getFileLegalLowerDir(strFileName);
            strNewFile = DiskReadZip_FilePacker._getFileLegalLowerDir(strNewFile);
            if (string.IsNullOrEmpty(strFileName) || string.IsNullOrEmpty(strNewFile)) return;//名称不能为空
            if (string.Compare(strFileName, strNewFile) == 0) return;//没有修改

            this.m_Conn = this.m_Packer.CheckConnection(this.Name);
            DiskReadZip_ConnectInfo conn = this.m_Conn;
            //ToDo:
            conn.Open();
            SharpCompress.Archive.Zip.ZipArchiveEntry findfile = null;

            foreach (var ze in conn.ZipTarget.Entries) {
                if (ze.IsDirectory) continue;
                if (string.Compare(ze.Key, strFileName, true) == 0) {
                    findfile = ze;
                    break;
                }
            }
            if (findfile != null) {
                //外面判断新文件是否存在
                MemoryStream ms = conn.GetUnCompressStream(findfile);
                conn.ZipTarget.AddEntry(strNewFile, ms, true, ms.Length, findfile.LastModifiedTime);
                conn.ZipTarget.RemoveEntry(findfile);
                findfile.Close();
            }
        }
 public int GetFiles(string strDir, out List<string> fileNames, out int totalSize)
 {
     fileNames = new List<string>();
     totalSize = 0;
     strDir = DiskReadZip_FilePacker._getFileLegalLowerDir(strDir);
     this.m_Conn = this.m_Packer.CheckConnection(this.Name);
     DiskReadZip_ConnectInfo conn = this.m_Conn;
     conn.Open();
     //所有的匹配目录名称
     List<SharpCompress.Archive.Zip.ZipArchiveEntry> filesInDir = new List<SharpCompress.Archive.Zip.ZipArchiveEntry>();
     if (string.IsNullOrEmpty(strDir)) {
         //最顶层 则是所有的文件
         foreach (var ze in conn.ZipTarget.Entries) {
             if (!ze.IsDirectory) {
                 filesInDir.Add(ze);
             }
         }
     }
     else {
         //非最顶层
         strDir = strDir + "/";
         //ToDo:查找里面的所有文件
         foreach (var ze in conn.ZipTarget.Entries) {
             if (!ze.IsDirectory) {
                 if (ze.Key.ToLower().StartsWith(strDir)) {
                     filesInDir.Add(ze);
                 }
             }
         }
     }
     foreach (var v in filesInDir) {
         fileNames.Add(v.Key);
         totalSize += (int)v.Size;
     }
     //
     int count = filesInDir.Count;
     return count;
 }
 public void UpdateFile(string strFileName, byte[] fileData, DateTime date)
 {
     strFileName = DiskReadZip_FilePacker._getFileLegalLowerDir(strFileName);
     if (string.IsNullOrEmpty(strFileName)) {
         throw new ArgumentNullException(strFileName);
     }
     //
     this.m_Conn = this.m_Packer.CheckConnection(this.Name);
     DiskReadZip_ConnectInfo conn = this.m_Conn;
     conn.Open();
     SharpCompress.Archive.Zip.ZipArchiveEntry findFile = null;
     foreach (var ze in conn.ZipTarget.Entries) {
         if (ze.IsDirectory) continue;
         if (string.Compare(ze.Key, strFileName, true) == 0) {
             findFile = ze;
             break;
         }
     }
     //
     if (findFile != null) {
         conn.ZipTarget.RemoveEntry(findFile);
         findFile.Close();//删除老的
         AddFile(strFileName, fileData, date);
     }
     else {
         AddFile(strFileName, fileData, date);
     }
 }
 public byte[] OpenFile(string strFileName)
 {
     strFileName = DiskReadZip_FilePacker._getFileLegalLowerDir(strFileName);
     if (string.IsNullOrEmpty(strFileName)) {
         throw new ArgumentNullException(strFileName);
     }
     this.m_Conn = this.m_Packer.CheckConnection(this.Name);
     DiskReadZip_ConnectInfo conn = this.m_Conn;
     conn.Open();
     SharpCompress.Archive.Zip.ZipArchiveEntry findFile = null;
     foreach (var v in conn.ZipTarget.Entries) {
         if (v.IsDirectory) continue;
         if (string.Compare(v.Key, strFileName, true) == 0) {
             findFile = v;
             break;
         }
     }
     byte[] buf = null;
     if (findFile != null) {
         using (MemoryStream ms = conn.GetUnCompressStream(findFile)) {
             buf = ms.ToArray();
             ms.Close();//关闭流
         }
     }
     return buf;
 }
        /// <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];
        }
        /// <summary>
        /// dir要规范,不要=='/',不要'/'开头,不要带'/'结尾
        /// </summary>
        /// <param name="strDirName"></param>
        /// <param name="strNewDirName"></param>
        public void RenameDir(string strDirName, string strNewDirName)
        {
            strDirName = DiskReadZip_FilePacker._getFileLegalLowerDir(strDirName);// +"/";//规范化
            strNewDirName = DiskReadZip_FilePacker._getFileLegalLowerDir(strNewDirName);// +"/";//规范化
            if (string.IsNullOrEmpty(strDirName) || string.IsNullOrEmpty(strNewDirName)) {
                return;//根目录名称不能改
            }
            if (string.Compare(strDirName, strNewDirName) == 0) {
                return;//没有改名
            }
            strDirName = strDirName + "/";
            strNewDirName = strNewDirName + "/";

            this.m_Conn = this.m_Packer.CheckConnection(this.Name);
            DiskReadZip_ConnectInfo conn = this.m_Conn;

            conn.Open();
            //修改所有的匹配目录名称
            List<SharpCompress.Archive.Zip.ZipArchiveEntry> olddirs = new List<SharpCompress.Archive.Zip.ZipArchiveEntry>();
            List<SharpCompress.Archive.Zip.ZipArchiveEntry> filesInDir = new List<SharpCompress.Archive.Zip.ZipArchiveEntry>();
            foreach (var ze in conn.ZipTarget.Entries) {
                if (ze.IsDirectory) {
                    if (ze.Key.ToLower().StartsWith(strDirName)) {
                        olddirs.Add(ze);
                    }
                }
                else {
                    if (ze.Key.ToLower().StartsWith(strDirName)) {
                        filesInDir.Add(ze);
                    }
                }
            }
            //外面判断是否已经存在新目录名
            //改名
            string oldfName = "";
            string newfName = "";
            for (int i = filesInDir.Count - 1; i >= 0; i--) {
                oldfName = filesInDir[i].Key;
                newfName = filesInDir[i].Key.Remove(0, strDirName.Length);
                newfName = strNewDirName + newfName;
                //
                MemoryStream ms = conn.GetUnCompressStream(filesInDir[i]);
                conn.ZipTarget.AddEntry(newfName, ms, true, ms.Length, filesInDir[i].LastModifiedTime);
                conn.ZipTarget.RemoveEntry(filesInDir[i]);
                filesInDir[i].Close();
            }
            //移除老文件夹
            for (int i = olddirs.Count - 1; i >= 0; i--) {
                conn.ZipTarget.RemoveEntry(olddirs[i]);
                olddirs[i].Close();
            }
        }
 //
 internal DiskReadZip_FileTable(DiskReadZip_FilePacker packer, DiskReadZip_ConnectInfo conn)
 {
     m_Conn = conn;
     //m_buf = arg;
     m_Packer = packer;
 }