/// <summary> /// 解压zip文件 /// </summary> /// <param name="stream">本解压的文件流</param> /// <returns>解压后的文件流</returns> public static byte[] UnZipFile(Stream stream) { using (ZipInputStream inputZipStream = new ZipInputStream(stream)) { ZipEntry theEntry = inputZipStream.GetNextEntry(); using (MemoryStream outZipStream = new MemoryStream()) { inputZipStream.CopyTo(outZipStream); return outZipStream.ToArray(); } } }
// part of a folder <- ZIP stream /// <summary> /// Unzips the specified ZIP stream to the new or existing folder. If the folder already exists, archive items will be added to the existing folder structure. /// Files will be overwritten when there is an existing file with the same name as an archive file. /// </summary> public static void UnZipStreamIntoFolder( Stream sourceStream, string destinationFolderPath ) { Directory.CreateDirectory( destinationFolderPath ); using( var zipInputStream = new ZipInputStream( sourceStream ) ) { ZipEntry entry; while( ( entry = zipInputStream.GetNextEntry() ) != null ) { if( entry.IsDirectory ) Directory.CreateDirectory( EwlStatics.CombinePaths( destinationFolderPath, entry.Name ) ); else { using( var outputStream = IoMethods.GetFileStreamForWrite( EwlStatics.CombinePaths( destinationFolderPath, entry.Name ) ) ) zipInputStream.CopyTo( outputStream ); } } } }
public static ZipContainer Unzip(Stream stream) { ZipEntry entry; var zippedFiles = new List<ZippedFile>(); var source = new ZipInputStream(stream); while ((entry = source.GetNextEntry()) != null) { if (entry.IsFile) { var destination = new MemoryStream(); source.CopyTo(destination); zippedFiles.Add(new ZippedFile(destination, entry.Name.Replace('\\', '/'))); } } return new ZipContainer(zippedFiles); }
/// <summary> /// This method completely ignores directory structure of the zip file in the source stream. The result is a flattened list of files. But, the file names do contain /// the relative path information, so they can be used to re-create the directory structure. /// </summary> public static IEnumerable<RsFile> UnZipStreamAsFileObjects( Stream source ) { var files = new List<RsFile>(); using( var zipInputStream = new ZipInputStream( source ) ) { ZipEntry entry; while( ( entry = zipInputStream.GetNextEntry() ) != null ) { if( entry.IsDirectory ) continue; using( var outputStream = new MemoryStream() ) { zipInputStream.CopyTo( outputStream ); files.Add( new RsFile( outputStream.ToArray(), entry.Name ) ); } } } return files; }
public override Stream OpenRead(string fileName) { MemoryStream ms = null; using (Stream stream = FileInfo.OpenRead()) using (ZipInputStream zis = new ZipInputStream(stream)) { ZipEntry entry; while ((entry = zis.GetNextEntry()) != null) { if (entry.IsFile && string.Equals(entry.Name, fileName, StringComparison.InvariantCultureIgnoreCase)) { ms = new MemoryStream((int)entry.Size); zis.CopyTo(ms, (int)entry.Size); ms.Seek(0, SeekOrigin.Begin); break; } } } return ms; }
/// <summary> /// Decompresses the given data stream from its source ZIP or GZIP format. /// </summary> /// <param name="dataBytes"></param> /// <returns></returns> internal static byte[] Inflate(Stream dataStream) { byte[] outputBytes = null; var zipInputStream = new ZipInputStream(dataStream); if (zipInputStream.CanDecompressEntry) { MemoryStream zipoutStream = new MemoryStream(); zipInputStream.CopyTo(zipoutStream); outputBytes = zipoutStream.ToArray(); } else { try { dataStream.Seek(0, SeekOrigin.Begin); #if !WINDOWS_PHONE var gzipInputStream = new GZipInputStream(dataStream, CompressionMode.Decompress); #else var gzipInputStream = new GZipInputStream(dataStream); #endif MemoryStream zipoutStream = new MemoryStream(); gzipInputStream.CopyTo(zipoutStream); outputBytes = zipoutStream.ToArray(); } catch (Exception exc) { CCLog.Log("Error decompressing image data: " + exc.Message); } } return outputBytes; }
public static void CheckScripts(Int64 build, String manifestFile) { var scripts = HoloXPLOR_App.Scripts; HoloXPLOR_App._scriptsPath = HostingEnvironment.MapPath(String.Format("~/App_Data/Scripts-{0}", build)); if (!Directory.Exists(HoloXPLOR_App._scriptsPath)) { Directory.CreateDirectory(HoloXPLOR_App._scriptsPath); } if (!File.Exists(Path.Combine(HoloXPLOR_App._scriptsPath, "game.xml"))) { using (WebClient client = new WebClient()) { // client.DownloadFile(manifestFile, Path.Combine(scriptsPath, String.Format("{0}.json", build))); var manifest = client.DownloadString(manifestFile).FromJSON<Manifest>(); #region Download DataXML.pak using (Stream pakStream = client.OpenRead(String.Format("{0}/{1}/{2}", manifest.WebseedUrls[HoloXPLOR_App._random.Next(manifest.WebseedUrls.Length)], manifest.KeyPrefix, "Data/DataXML.pak"))) { using (var zipStream = new ZipInputStream(pakStream)) { ZipEntry zipEntry = zipStream.GetNextEntry(); while (zipEntry != null) { String entryFileName = zipEntry.Name; if (entryFileName.StartsWith("Scripts")) { String fullZipToPath = Path.Combine(HoloXPLOR_App._scriptsPath, entryFileName).Replace(@"\Scripts\", @"\"); String directoryName = Path.GetDirectoryName(fullZipToPath); if (directoryName.Length > 0) { Directory.CreateDirectory(directoryName); } using (var ms = new MemoryStream()) { zipStream.CopyTo(ms); ms.Seek(0, SeekOrigin.Begin); var xml = CryXmlSerializer.ReadStream(ms); xml.Save(fullZipToPath); } } zipEntry = zipStream.GetNextEntry(); } } } #endregion #region Download GameData.pak using (Stream pakStream = client.OpenRead(String.Format("{0}/{1}/{2}", manifest.WebseedUrls[HoloXPLOR_App._random.Next(manifest.WebseedUrls.Length)], manifest.KeyPrefix, "Data/GameData.pak"))) { using (var zipStream = new ZipInputStream(pakStream)) { ZipEntry zipEntry = zipStream.GetNextEntry(); while (zipEntry != null) { String entryFileName = zipEntry.Name; if (entryFileName.EndsWith(".dcb")) { String fullZipToPath = Path.Combine(HoloXPLOR_App._scriptsPath, "game.xml"); using (var ms = new MemoryStream()) { zipStream.CopyTo(ms); ms.Seek(0, SeekOrigin.Begin); using (var br = new BinaryReader(ms)) { var df = new DataForge.DataForge(br); // df.GenerateXML(); df.Save(fullZipToPath); } } } zipEntry = zipStream.GetNextEntry(); } } } #endregion } scripts = new Scripts(HoloXPLOR_App._scriptsPath); } scripts = scripts ?? new Scripts(HoloXPLOR_App._scriptsPath); if (scripts.Ammo.Count > 0 & scripts.Items.Count > 0 && scripts.Loadout.Count > 0 && scripts.Localization.Count > 0 && scripts.Vehicles.Count > 0) { String latestBuildFile = HostingEnvironment.MapPath("~/App_Data/latestBuild.txt"); File.WriteAllText(latestBuildFile, HoloXPLOR_App._scriptsPath); HoloXPLOR_App.Scripts = scripts; } }
List<TestCase> AddTestCase(Contest contest, Problem problem, HttpPostedFileBase file) { if (file == null) { ModelState.AddModelError("File", "请选择文件"); return null; } Dictionary<int, byte[]> inputFiles = new Dictionary<int, byte[]>(); Dictionary<int, byte[]> outputFiles = new Dictionary<int, byte[]>(); if (new[] { "application/zip", "application/x-zip-compressed", "application/x-zip" }.Contains(file.ContentType) || file.ContentType == "application/octet-stream" && file.FileName.EndsWith(".zip", StringComparison.OrdinalIgnoreCase)) { using (ZipInputStream stream = new ZipInputStream(file.InputStream)) { ZipEntry entry; while ((entry = stream.GetNextEntry()) != null) { byte[] bytes; using (MemoryStream mem = new MemoryStream()) { stream.CopyTo(mem); bytes = mem.ToArray(); } if (!DealEntry(entry.Name, bytes, inputFiles, outputFiles)) { return null; } } } } else if (file.FileName.EndsWith(".tgz") || file.FileName.EndsWith(".tar.gz")) { using (GZipStream stream = new GZipStream(file.InputStream, CompressionMode.Decompress)) { using (TarInputStream tar = new TarInputStream(stream)) { TarEntry entry; while ((entry = tar.GetNextEntry()) != null) { byte[] bytes; using (MemoryStream mem = new MemoryStream()) { tar.CopyTo(mem); bytes = mem.ToArray(); } if (!DealEntry(entry.Name, bytes, inputFiles, outputFiles)) { return null; } } } } } else if (file.FileName.EndsWith(".tar.bz2")) { using (BZip2InputStream stream = new BZip2InputStream(file.InputStream)) { using (TarInputStream tar = new TarInputStream(stream)) { TarEntry entry; while ((entry = tar.GetNextEntry()) != null) { byte[] bytes; using (MemoryStream mem = new MemoryStream()) { tar.CopyTo(mem); bytes = mem.ToArray(); } if (!DealEntry(entry.Name, bytes, inputFiles, outputFiles)) { return null; } } } } } else { ModelState.AddModelError("File", "不支持的压缩文件类型"); return null; } if (!inputFiles.Keys.OrderBy(x => x).SequenceEqual(outputFiles.Keys.OrderBy(x => x))) { ModelState.AddModelError("File", "输入与输出文件没有一一对应"); return null; } var testCases = inputFiles.Keys.Select(id => new TestCase { Input = inputFiles[id], Data = outputFiles[id], MemoryLimit = DEFAULT_TEST_CASE_MEMORY_LIMIT, TimeLimit = DEFAULT_TEST_CASE_TIME_LIMIT, Available = contest.Type == Contest.ContestType.CF ? false : true }).ToList(); foreach (var t in testCases) { t.ID = problem.AddTestCase(t); } return testCases; }
/// <summary> /// Decompresses the given data stream from its source ZIP or GZIP format. /// </summary> /// <param name="dataBytes"></param> /// <returns></returns> private static byte[] Inflate(byte[] dataBytes) { byte[] outputBytes = null; var zipInputStream = new ZipInputStream(new MemoryStream(dataBytes)); if (zipInputStream.CanDecompressEntry) { MemoryStream zipoutStream = new MemoryStream(); #if XBOX byte[] buf = new byte[4096]; int amt = -1; while (true) { amt = zipInputStream.Read(buf, 0, buf.Length); if (amt == -1) { break; } zipoutStream.Write(buf, 0, amt); } #else zipInputStream.CopyTo(zipoutStream); #endif outputBytes = zipoutStream.ToArray(); } else { try { var gzipInputStream = new GZipInputStream(new MemoryStream(dataBytes)); MemoryStream zipoutStream = new MemoryStream(); #if XBOX byte[] buf = new byte[4096]; int amt = -1; while (true) { amt = gzipInputStream.Read(buf, 0, buf.Length); if (amt == -1) { break; } zipoutStream.Write(buf, 0, amt); } #else gzipInputStream.CopyTo(zipoutStream); #endif outputBytes = zipoutStream.ToArray(); } catch (Exception exc) { CCLog.Log("Error decompressing image data: " + exc.Message); } } return outputBytes; }
public Result<bool> BulkImportSourcePages(string aSourcePageId, Stream file, Result<bool> aResult) { bool valid = TestFacsimileZipStream(file); if (!valid) { aResult.Return(false); return aResult; } file.Position = 0; using (ZipInputStream zis = new ZipInputStream(file)) { ZipEntry entry; while ((entry = zis.GetNextEntry()) != null) { if (!entry.IsFile) continue; Match match = Regex.Match(entry.Name, @"\$facsimile_(\d+)\.jpg"); string number; number = match.Groups[1].Value; ISourcePage page = Context.Current.Instance.SourcePageController.CreateNew(); page.PageNumber = int.Parse(number); page.DisplayPageNumber = int.Parse(number); page.SourceId = aSourcePageId; page = Context.Current.Instance.SourcePageController.Insert(page, new Result<ISourcePage>()).Wait(); using (Stream fasc = new MemoryStream()) { zis.CopyTo(fasc, zis.Length); fasc.Position = 0; Context.Current.Instance.SourcePageController.AddFascimile(page.Id, fasc,fasc.Length, new Result<bool>()).Wait(); } } } aResult.Return(true); return aResult; }
/// <summary> /// Decompresses the given data stream from its source ZIP or GZIP format. /// </summary> /// <param name="dataBytes"></param> /// <returns></returns> internal static byte[] Inflate(Stream dataStream, CompressionFormat format) { byte[] outputBytes = null; switch (format) { case CompressionFormat.Zlib: try { var zipInputStream = new ZipInputStream (dataStream); if (zipInputStream.CanDecompressEntry) { MemoryStream zipoutStream = new MemoryStream (); zipInputStream.CopyTo (zipoutStream); outputBytes = zipoutStream.ToArray (); } else { dataStream.Seek(0, SeekOrigin.Begin); var inZInputStream = new ZInputStream(dataStream); var outMemoryStream = new MemoryStream(); outputBytes = new byte[inZInputStream.BaseStream.Length]; while (true) { int bytesRead = inZInputStream.Read(outputBytes, 0, outputBytes.Length); if (bytesRead == 0) break; outMemoryStream.Write(outputBytes, 0, bytesRead); } } } catch (Exception exc) { CCLog.Log("Error decompressing image data: " + exc.Message); } break; case CompressionFormat.Gzip: try { #if !WINDOWS_PHONE var gzipInputStream = new GZipInputStream(dataStream, CompressionMode.Decompress); #else var gzipInputStream = new GZipInputStream(dataStream); #endif MemoryStream zipoutStream = new MemoryStream (); gzipInputStream.CopyTo (zipoutStream); outputBytes = zipoutStream.ToArray (); } catch (Exception exc) { CCLog.Log ("Error decompressing image data: " + exc.Message); } break; } return outputBytes; }