private void extractSelectedToolStripMenuItem_Click(object sender, EventArgs e) { var selItems = this.lvwFiles.SelectedItems.Cast <ListViewItem>(); if (selItems.Count() == 0) { return; } var fileDatas = selItems.Where(item => item.Tag is FileData).Select(item => item.Tag as FileData).ToList(); var dirDatas = selItems.Where(item => item.Tag is DirData).Select(item => item.Tag as DirData).ToList(); if (this.dlgFolder.ShowDialog(this) != DialogResult.OK) { return; } string unzipDir = this.dlgFolder.SelectedPath; int decfileCnt = ZipData.UnzipFile(fileDatas, dirDatas, this.zipFilePath, unzipDir); var t1 = DateTime.Now; var dt = DateTime.Now - t1; MessageBox.Show($"{decfileCnt} files unziped to {unzipDir}.\r\nelapse time : {dt.TotalSeconds:0.0}s"); }
private static void MakeStructure(ZipData zipData, List <FileData> fileDatas) { foreach (var fileData in fileDatas) { ZipDataAddFileData(zipData, fileData); } }
private void extractAllToolStripMenuItem_Click(object sender, EventArgs e) { if (this.zipFilePath == null || this.trvZip.Nodes.Count == 0) { return; } var dirData = this.trvZip.Nodes[0].Tag as DirData; if (dirData == null) { return; } if (this.dlgFolder.ShowDialog(this) != DialogResult.OK) { return; } string unzipDir = this.dlgFolder.SelectedPath; var t1 = DateTime.Now; int decfileCnt = ZipData.UnzipFile(dirData.fileDatas, dirData.dirDatas, this.zipFilePath, unzipDir); var dt = DateTime.Now - t1; MessageBox.Show($"{decfileCnt} files unziped to {unzipDir}.\r\nelapse time : {dt.TotalSeconds:0.0}s"); }
public static ZipData MakeZipData(string[] files, string[] dirs, ref int fileCnt, ref long totalByte) { ZipData zipData = new ZipData(); zipData.dirData = new DirData(); MakeDirDataRecursive(files, dirs, zipData.dirData, ref fileCnt, ref totalByte); return(zipData); }
// dirData -> tree private void ZipToTree(ZipData zipData, string rootName) { this.trvZip.Nodes.Clear(); var node = this.trvZip.Nodes.Add(rootName); node.Tag = zipData.dirData; this.DirDataToTree(zipData.dirData, node.Nodes); this.trvZip.SelectedNode = node; node.Expand(); }
// ==== write zip file ==== // zipData => zip file public static int ZipFile(ZipData zipData, string zipFilePath) { var sr = File.OpenWrite(zipFilePath); using (BinaryWriter bw = new BinaryWriter(sr)) { // header bw.Write(Encoding.ASCII.GetBytes(ShimZip.magic)); bw.Write(ShimZip.version); // write file recursive return(ZipFileRecursive(zipData.dirData.fileDatas, zipData.dirData.dirDatas, bw)); } }
// ==== read directory structure ==== public static ZipData GetZipData(string zipFilePath, ref int fileCnt, ref long totalByte) { ZipData zipData = new ZipData(); using (var sr = File.OpenRead(zipFilePath)) using (var br = new BinaryReader(sr)) { string magic = Encoding.ASCII.GetString(br.ReadBytes(4)); var version = br.ReadInt16(); if (magic != ShimZip.magic || version > ShimZip.version) { throw new Exception("Invalid ShimZip header"); } zipData.dirData = new DirData(); GetDirDataRecursive(zipData.dirData, br, ref fileCnt, ref totalByte); } return(zipData); }
//// ==== write zip file ==== //// zipData => zip file //public static int ZipFile(ZipData zipData, string zipFilePath) { // var sr = File.OpenWrite(zipFilePath); // using (BinaryWriter bw = new BinaryWriter(sr)) { // // header // bw.Write(Encoding.ASCII.GetBytes(QuakePak.magic)); // bw.Write(QuakePak.version); // // write file recursive // return ZipFileRecursive(zipData.dirData.fileDatas, zipData.dirData.dirDatas, bw); // } //} //// recursive //private static int ZipFileRecursive(List<FileData> fileDatas, List<DirData> dirDatas, BinaryWriter bw) { // int fileCnt = fileDatas.Count; // // files // bw.Write(fileDatas.Count); // foreach (var fileData in fileDatas) { // bw.Write(fileData.name); // bw.Write(fileData.length); // bw.Write(fileData.creationTimeUtc.ToBinary()); // bw.Write(fileData.lastAccessTimeUtc.ToBinary()); // bw.Write(fileData.lastWriteTimeUtc.ToBinary()); // EncodeFile(fileData.realPath, bw); // } // // dirs // bw.Write(dirDatas.Count); // foreach (var dirData in dirDatas) { // bw.Write(dirData.name); // bw.Write(dirData.creationTimeUtc.ToBinary()); // bw.Write(dirData.lastAccessTimeUtc.ToBinary()); // bw.Write(dirData.lastWriteTimeUtc.ToBinary()); // fileCnt += ZipFileRecursive(dirData.fileDatas, dirData.dirDatas, bw); // } // return fileCnt; //} //// file encoding //private static void EncodeFile(string filePath, BinaryWriter bw) { // using (var stream = File.OpenRead(filePath)) { // byte[] blockBuf = new byte[BlockSize]; // int readSize; // while ((readSize = stream.Read(blockBuf, 0, BlockSize)) > 0) { // bw.Write(blockBuf, 0, readSize); // } // } //} // ==== read directory structure ==== public static ZipData GetZipData(string zipFilePath, ref int fileCnt, ref long totalByte) { ZipData zipData = new ZipData(); using (var sr = File.OpenRead(zipFilePath)) using (var br = new BinaryReader(sr)) { string magic = Encoding.ASCII.GetString(br.ReadBytes(4)); if (magic != QuakePak.magic) { throw new Exception("Invalid PAK header"); } zipData.dirData = new DirData(); int tableOffset = br.ReadInt32(); int tableSize = br.ReadInt32(); int fileDataSize = 64; fileCnt = tableSize / fileDataSize; int filePathSize = 56; List <FileData> fileDatas = new List <FileData>(); totalByte = 0; br.BaseStream.Seek(tableOffset, SeekOrigin.Begin); for (int i = 0; i < fileCnt; i++) { var bytes = br.ReadBytes(filePathSize); int nullIndex = Array.IndexOf <byte>(bytes, 0); string filePath = Encoding.ASCII.GetString(bytes, 0, nullIndex); string fileName = Path.GetFileName(filePath); int fileOffset = br.ReadInt32(); int fileSize = br.ReadInt32(); totalByte += fileSize; FileData fileData = new FileData(filePath, fileOffset, fileSize, new DateTime(), new DateTime(), new DateTime()); fileDatas.Add(fileData); } MakeStructure(zipData, fileDatas); } return(zipData); }
private void OpenZipFile(string zipFilePath) { this.zipFilePath = zipFilePath; int fileCnt = 0; long totalByte = 0; string ext = Path.GetExtension(zipFilePath).ToLower(); ZipData zipData = null; if (ext == ".sip") { zipData = ShimZip.GetZipData(this.zipFilePath, ref fileCnt, ref totalByte); } else if (ext == ".pak") { zipData = QuakePak.GetZipData(this.zipFilePath, ref fileCnt, ref totalByte); } string zipFileName = Path.GetFileName(zipFilePath); this.ZipToTree(zipData, Path.GetFileName(zipFilePath)); }
private static void ZipDataAddFileData(ZipData zipData, FileData fileData) { var tokens = fileData.name.Split('/'); fileData.name = tokens.Last(); fileData.realPath = string.Empty; var dirs = tokens.Take(tokens.Length - 1).ToList(); DirData dirData = zipData.dirData; foreach (var dir in dirs) { DirData findDirData = dirData.dirDatas.Find(subDir => subDir.name == dir); if (findDirData == null) { findDirData = new DirData(dir, new DateTime(), new DateTime(), new DateTime()); dirData.dirDatas.Add(findDirData); } dirData = findDirData; } dirData.fileDatas.Add(fileData); }
// == event handler private void zipToolStripMenuItem_Click(object sender, EventArgs e) { if (this.dlgFolder.ShowDialog(this) != DialogResult.OK) { return; } string dir = this.dlgFolder.SelectedPath; this.dlgSave.InitialDirectory = Path.GetDirectoryName(dir); this.dlgSave.FileName = Path.GetFileName(dir) + ".sip"; if (this.dlgSave.ShowDialog(this) != DialogResult.OK) { return; } string zipFilePath = this.dlgSave.FileName; var files = Directory.GetFiles(dir); var dirs = Directory.GetDirectories(dir); var t1 = DateTime.Now; int fileCnt = 0; long totalByte = 0; var zipData = ZipData.MakeZipData(files, dirs, ref fileCnt, ref totalByte); int encFileCnt = 0; if (Path.GetExtension(zipFilePath).ToLower() == ".sip") { encFileCnt = ShimZip.ZipFile(zipData, zipFilePath); } else { } var dt = DateTime.Now - t1; MessageBox.Show($"{encFileCnt} files ziped to {zipFilePath}.\r\nelapse time : {dt.TotalSeconds:0.0}s"); }