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); }
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(); } }
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); }
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); }
/// <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 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> /// 返回规范化的目录名称 不以'/'开头和结尾 /// </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 AddFile(DiskReadZip_FileItemInfo file) { this.Open(); //ToDo: MemoryStream ms = new MemoryStream(file.FileData); string filefullpath = DiskReadZip_FilePacker._getFileLegalLowerDir(Path.Combine(file.FileDir, file.FileName)); this.ZipTarget.AddEntry(filefullpath, ms, true, ms.Length, file.DateTimeFromStr(file.FileUpdateTime)); }
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="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 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); } }
// internal DiskReadZip_FileTable(DiskReadZip_FilePacker packer, DiskReadZip_ConnectInfo conn) { m_Conn = conn; //m_buf = arg; m_Packer = packer; }
internal static DiskReadZip_FilePacker OpenPacker(string rootDir) { DiskReadZip_FilePacker packer = new DiskReadZip_FilePacker(rootDir); return(packer); }
internal static DiskReadZip_FilePacker OpenPacker(string rootDir) { DiskReadZip_FilePacker packer = new DiskReadZip_FilePacker(rootDir); return 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(); } }