public FileAPIResponse DownloadFile()
 {
     try{
         GetResponseFromServer();
         FileAPIResponse fileAPIResponse = new FileAPIResponse(response);
         return(fileAPIResponse);
     }
     catch (Exception e) when(!(e is ZCRMException))
     {
         ZCRMLogger.LogError(e);
         throw new ZCRMException(e);
     }
 }
 protected bool WriteStreamtoZipFile(FileAPIResponse fileResponse, string filePath)
 {
     try
     {
         Stream file = fileResponse.GetFileAsStream();                            //To get attachment file content
         CommonUtil.SaveStreamAsFile(filePath, file, fileResponse.GetFileName()); //To write a new file using the same attachment name
         file.Close();
     }
     catch (Exception)
     {
         return(false);
     }
     return(true);
 }
        public BulkResponse ProcessZip(string filePath, bool download, string fileName, string operationType, string fileURL, bool checkFailed)
        {
            StreamReader  streamReader             = null;
            string        fileType                 = "";
            List <string> fieldAPINames            = new List <string>();
            Dictionary <string, object> EventsData = new Dictionary <string, object>();
            string ModuleAPIName;

            try
            {
                if (operationType.Equals(APIConstants.READ))
                {
                    if (download && fileName == null)
                    {
                        FileAPIResponse fileResponse = BulkReadAPIHandler.GetInstance(this.bulkReadRecordIns).DownloadBulkReadResult();
                        if (!fileResponse.HttpStatusCode.Equals(APIConstants.ResponseCode.OK))
                        {
                            throw new ZCRMException("Zip file not downloaded");
                        }
                        else
                        {
                            if (!this.WriteStreamtoZipFile(fileResponse, filePath + "/"))
                            {
                                throw new ZCRMException("Error while writing file in the file path specified: " + filePath + "/");
                            }
                            if (!this.Unzip(filePath + "/" + fileResponse.GetFileName(), filePath + "/"))
                            {
                                throw new ZCRMException("Error occurred while unzipping the file: " + filePath + "/" + fileResponse.GetFileName());
                            }
                            streamReader = new StreamReader(File.OpenRead(filePath + "/" + this.fileName));
                        }
                        fileResponse = null;
                    }
                    ModuleAPIName = this.bulkReadRecordIns.ModuleAPIName;
                }
                else
                {
                    if (download && fileName == null && fileURL != null)
                    {
                        FileAPIResponse fileResponse = BulkWriteAPIHandler.GetInstance(this.bulkWriteRecordIns).DownloadBulkWriteResult(fileURL);
                        if (!fileResponse.HttpStatusCode.Equals(APIConstants.ResponseCode.OK))
                        {
                            throw new ZCRMException("Zip file not downloaded");
                        }
                        else
                        {
                            if (!this.WriteStreamtoZipFile(fileResponse, filePath + "/"))
                            {
                                throw new ZCRMException("Error while writing file in the file path specified: " + filePath + "/");
                            }

                            if (!this.Unzip(filePath + "/" + fileResponse.GetFileName(), filePath + "/"))
                            {
                                throw new ZCRMException("Error occurred while unzipping the file: " + filePath + "/" + fileResponse.GetFileName());
                            }
                            streamReader = new StreamReader(File.OpenRead(filePath + "/" + this.fileName));
                        }
                        fileResponse = null;
                    }
                    ModuleAPIName = this.bulkWriteRecordIns.ModuleAPIName;
                }
                if (streamReader == null && fileName != null)
                {
                    if (!Unzip(filePath + "/" + fileName, filePath + "/"))
                    {
                        throw new ZCRMException("Error occurred while unzipping the file: " + filePath + "/" + fileName);
                    }
                    streamReader = new StreamReader(File.OpenRead(filePath + "/" + this.fileName));
                }
                try
                {
                    if (this.fileName.Contains(".ics"))
                    {
                        fileType = "ics";
                        long cur_pos = 0;
                        int  length  = 0;
                        while (!streamReader.EndOfStream)
                        {
                            string line = streamReader.ReadLine();
                            cur_pos = cur_pos + line.Length;
                            var value = line.Split(new[] { ':' }, 2);
                            if (!EventsData.ContainsKey(value[0]))
                            {
                                EventsData[value[0]] = value[1];
                            }
                            else
                            {
                                break;
                            }
                            length = value.Length;
                        }
                        streamReader.BaseStream.Seek(cur_pos, SeekOrigin.Begin);
                        streamReader.DiscardBufferedData();
                    }
                    else
                    {
                        string header = streamReader.ReadLine();
                        if (header != null)
                        {
                            fieldAPINames = this.ParseCsvRow(header);
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw new ZCRMException(APIConstants.SDK_ERROR, ex);
                }
            }
            catch (ZCRMException e)
            {
                ZCRMLogger.LogError(e);
                throw new ZCRMException(APIConstants.SDK_ERROR, e);
            }
            this.fileName = null;
            BulkResponse bulkResponse = new BulkResponse(ModuleAPIName, streamReader, checkFailed, fileType);

            bulkResponse.FieldAPINames = fieldAPINames;
            bulkResponse.APIHandlerIns = this;
            if (fileType.Equals("ics"))
            {
                EventsData["EventsData"] = bulkResponse;
                EventsData["END"]        = (string)EventsData["BEGIN"];
                bulkResponse.Data        = EventsData;
            }
            return(bulkResponse);
        }