在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
{ Packer.Pack(@"c:\temp\all.dat", @"c:\temp\a.bat", @"c:\temp\a.jpg"); string[] resultFiles = Packer.Unpack( @"c:\temp\all.dat" ); MessageBox.Show( string.Join("\n", resultFiles), "files generated under the current directory"); } public class Packer { class Header { public long length; public string filename; public void WriteTo( Stream fs ) { byte[] len = BitConverter.GetBytes(length); byte[] buf = new byte[256]; byte[] str = Encoding.Unicode.GetBytes(filename); str.CopyTo(buf, 0); fs.Write(len, 0, len.Length); fs.Write(buf, 0, buf.Length); } public bool ReadFrom( Stream fs) { byte[] len = BitConverter.GetBytes(length); byte[] buf = new byte[256]; if (len.Length != fs.Read(len, 0, len.Length)) return false; if (buf.Length != fs.Read(buf, 0, buf.Length)) return false; length = BitConverter.ToInt64(len, 0); filename = Encoding.Unicode.GetString(buf).Trim(new char[] { '\0' }); return true; } } public static void Pack(string resultFilename, params string[] filenames) { using (FileStream fout = new FileStream(resultFilename, FileMode.Create, FileAccess.Write)) { for (int i = 0; i < filenames.Length; i++) { using (FileStream fin = new FileStream(filenames[i], FileMode.Open)) { Header header = new Header(); header.length = fin.Length; header.filename = Path.GetFileName(filenames[i]); header.WriteTo(fout); byte[] buf = new byte[header.length]; fin.Read(buf, 0, buf.Length); fout.Write(buf, 0, buf.Length); } } } } public static string[] Unpack(string filename) { List<string> unpackedFiles = new List<string>(); using (FileStream fin = new FileStream(filename, FileMode.Open, FileAccess.Read)) { Header header = new Header(); while (header.ReadFrom(fin)) { unpackedFiles.Add(header.filename); byte[] buf = new byte[header.length]; fin.Read(buf, 0, buf.Length); using (FileStream fout = new FileStream(header.filename, FileMode.Create, FileAccess.Write)) { fout.Write(buf, 0, buf.Length); } } } return unpackedFiles.ToArray(); } } |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论