Zip 压缩问题件,获取真实扩展名
var _ZIP = new System.IO.Compression.ZipArchive(Request.Files[0].InputStream); foreach (var element in _ZIP.Entries) { using (System.IO.StreamReader _ddd = new System.IO.StreamReader(element.Open())) { string _Text = _ddd.ReadToEnd(); } using (System.IO.BinaryReader _ddd = new System.IO.BinaryReader(element.Open())) { string bx = " "; byte buffer; buffer = r.ReadByte(); bx = buffer.ToString(); buffer = r.ReadByte(); bx += buffer.ToString(); //bx == 255216 jpg 파일 } }
static void Main(string[] args) { string str = string.Empty; str= Console.ReadLine(); Console.WriteLine("length:" + str.Length); str=ToZip(str); Console.WriteLine("ToZip:" + str); Console.WriteLine("length:" + str.Length); str= ToUnzip(str); Console.WriteLine("ToUnzip:" + str); Console.WriteLine("length:" + str.Length); Console.Read(); } static string ToZip(string _str) { string result = string.Empty; byte[] bString = Encoding.UTF8.GetBytes(_str); using (var msi = new System.IO.MemoryStream(bString)) using (var mso = new System.IO.MemoryStream()) { using (var gs = new System.IO.Compression.GZipStream(mso, System.IO.Compression.CompressionMode.Compress)) { byte[] bTemp = new byte[4096]; int count; while ((count = msi.Read(bTemp, 0, bTemp.Length)) != 0) { gs.Write(bTemp, 0, count); } } result= System.Convert.ToBase64String(mso.ToArray()); } return result; } static string ToUnzip(string _str) { string result = string.Empty; byte[] bString = System.Convert.FromBase64String(_str); using (var msi = new System.IO.MemoryStream(bString)) using (var mso = new System.IO.MemoryStream()) { using (var gs = new System.IO.Compression.GZipStream(msi, System.IO.Compression.CompressionMode.Decompress)) { byte[] bTemp = new byte[4096]; int count; while ((count = gs.Read(bTemp, 0, bTemp.Length)) != 0) { mso.Write(bTemp, 0, count); } } result= Encoding.UTF8.GetString(mso.ToArray()); } return result; }