private void Build(T t, string Read = "data/pangya_gb.iff") { var zip = ZipFile.OpenRead(Read); //ler o arquivo de base FileZip = zip.Entries.FirstOrDefault(c => c.Name == t.FileName); //verifica se existe o arquivo if (FileZip != null) { MemoryStream memory = new MemoryStream(); FileZip.Open().CopyTo(memory);//lê os bytes do stream atual, iniciando na posição atual, e os grava em outro stream using (BinaryReader reader = new BinaryReader(memory)) { if ((IsZIPFile = (new string(reader.ReadChars(2)) == "PK")) == true) { return; } reader.BaseStream.Seek(0, SeekOrigin.Begin); RecordCount = reader.ReadUInt16(); RecordLength = ((reader.BaseStream.Length - 8L) / (RecordCount)); BindingID = reader.ReadUInt16(); Version = reader.ReadUInt32(); for (int i = 0; i < RecordCount; i++) { reader.BaseStream.Seek(8L + (RecordLength * i), SeekOrigin.Begin); byte[] recordData = reader.ReadBytes((int)RecordLength); t.SetReader(new BinaryReader(new MemoryStream(recordData))); //save iff var model = (T)t.Get(); Add(model); } t.CloseBinary(); } } else { Console.WriteLine(" File No Exist: " + Read + "/" + t.FileName); } }
public async Task <ActionResult> UploadFile([FromForm] FileZip f) { Directory.CreateDirectory("App_Data"); Directory.CreateDirectory("Games"); Game game; string gameID = f.ID; string name = f.Name; string description = f.description; string minP = f.minP; string maxP = f.maxP; string image = f.imageName; string le = f.LastEdited; var file = f.File; var filePath = Path.Combine("App_Data", f.FileName); await Task.Run(() => { if (file.Length > 0) { using var fileStream = file.OpenReadStream(); using ZipFile zip = ZipFile.Read(fileStream); foreach (ZipEntry e in zip) { e.Extract(@$ "App_Data/{f.FileName.Replace(".zip ", " ")}"); } } }); foreach (var inFile in Directory.GetFiles(filePath.Replace(".zip", ""))) { string fileHash = (await System.IO.File.ReadAllBytesAsync(inFile)).SHA1(); if (fileHash == Path.GetFileName(inFile)) { await Task.Run(() => { if (!System.IO.File.Exists($"Games/{Path.GetFileName(inFile)}")) { System.IO.File.Move(inFile, $"Games/{Path.GetFileName(inFile)}"); System.IO.File.Delete(inFile); } else { System.IO.File.Delete(inFile); } }); } else { return(BadRequest()); } } Directory.Delete(filePath.Replace(".zip", "")); if (int.Parse(gameID) == -1 || (game = await Context.Game.FindAsync(int.Parse(gameID))) == null) { game = new Game { Creator = await Context.User.FindAsync(LoggedUser.Id) }; await Context.Game.AddAsync(game); } game.Name = name; game.Hash = f.FileName.Replace(".zip", ""); game.Description = description; game.Minplayers = int.Parse(minP); game.Maxplayers = int.Parse(maxP); game.Image = image; game.LastEdited = le; await Context.SaveChangesAsync(); game.Status = new GameParser().Parse(await System.IO.File.ReadAllLinesAsync($"Games/{game.Hash}")) == ParsingError.Ok ? 3 : 2; return(Ok(new { ID = game.Id, game.Status })); }
/// <summary> </summary> /// /// <param name="pathCompressedFile"></param> /// <param name="createFile"> Crear fichero destino. </param> /// <param name="CompressionLvl"> Nivel de compresión. </param> /// <param name="ListaDeFicheros"> Lista de ficheros / carpetas a comprimir. </param> /// /// <returns></returns> /// public Char FullCompress(string pathCompressedFile, bool createFile = false, int CompressionLvl = (int)CompressionLevel.Fastest, params object [] ListaDeFicheros) { Char resultado = 'N'; ZipArchive FileZip; string currentFile; string currentDirectory; int totalArchivosProcesados = 0; try { UsrError = "KO"; UsrErrorC = 0; UsrErrorE = ""; if (ListaDeFicheros.Count() <= 0) { // Sin archivos a comprimir. UsrError = "KO - No hay archivos a comprimir"; UsrErrorC = -1; resultado = 'N'; return(resultado); } // Eliminar, si cal, el fichero Zip destino. if (System.IO.File.Exists(pathCompressedFile) && createFile == true) { System.IO.File.Delete(pathCompressedFile); } FileZip = ZipFile.Open(pathCompressedFile, ZipArchiveMode.Update); for (int i = 0; i < ListaDeFicheros.Length; i++) { currentDirectory = Path.GetDirectoryName((string)ListaDeFicheros[i]); currentFile = Path.GetFileName((string)ListaDeFicheros[i]); // Agregar la carpeta de trabajo si el parámetros contiene solo el nombre de fichero. if (String.IsNullOrEmpty(currentDirectory)) { currentDirectory = Directory.GetCurrentDirectory(); } foreach (string currentFileToCompress in System.IO.Directory.GetFiles(currentDirectory, currentFile)) { FileZip.CreateEntryFromFile(currentFileToCompress, Path.GetFileName(currentFileToCompress), (CompressionLevel)CompressionLvl); totalArchivosProcesados += 1; } } FileZip.Dispose(); resultado = 'S'; UsrError = string.Format("OK - Total archivos comprimidos {0}", totalArchivosProcesados); } catch (Exception ex) { // Error en Zip / UnZip. UsrErrorC = ex.HResult; UsrError = ex.Message; UsrErrorE = ex.StackTrace; resultado = 'C'; } return(resultado); }
public FileZipEntry(string internalFilePath, FileZip zip) { this.zip = zip; entry = zip.GetEntryUnwrapped(internalFilePath); }