Exemplo n.º 1
0
        public static ReturnValor ZipFile(string fileNameToAdd, string pathDestino, string pnameFileZip = "", bool FlagConvertToBase64String = false)
        {
            var returnZip = new ReturnValor();

            string[] Names       = fileNameToAdd.Split('.');
            string   FileZipName = string.IsNullOrWhiteSpace(pnameFileZip) ?
                                   string.Concat("FileZip_", HelpTime.ConvertHHMMSSMM(DateTime.Now), ".zip") :
                                   string.Concat(pnameFileZip, ".zip");

            try
            {
                using (FileStream fOrigen = File.OpenRead(fileNameToAdd))
                {
                    using (FileStream fDestino = File.Create(Path.Combine(pathDestino, FileZipName)))
                    {
                        byte[] buffer = new byte[fOrigen.Length];
                        fOrigen.Read(buffer, 0, buffer.Length);

                        using (GZipStream output = new GZipStream(fDestino, CompressionMode.Compress))
                        {
                            output.Write(buffer, 0, buffer.Length);
                        }

                        returnZip.Exitosa       = true;
                        returnZip.CodigoRetorno = "OK";
                        returnZip.Message       = FileZipName;

                        if (FlagConvertToBase64String)
                        {
                            returnZip.ErrorCode = Convert.ToBase64String(File.ReadAllBytes(Path.Combine(pathDestino, FileZipName)));
                        }
                    }
                }
            }
            catch (Exception zp)
            {
                returnZip.Exitosa = false;
                returnZip.Message = "Error [Zip] " + zp.Message;
            }
            return(returnZip);
        }