Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"End of Central Directory record could not be found" Random Error during CreateReader #657

Open
mikegit75 opened this issue Oct 23, 2023 · 1 comment

Comments

@mikegit75
Copy link

I'm getting this random error
System.IO.InvalidDataException: End of Central Directory record could not be found. at System.IO.Compression.ZipArchive.ReadEndOfCentralDirectory() at System.IO.Compression.ZipArchive..ctor(Stream stream, ZipArchiveMode mode, Boolean leaveOpen, Encoding entryNameEncoding) at ExcelDataReader.Core.ZipWorker..ctor(Stream fileStream) at ExcelDataReader.ExcelOpenXmlReader..ctor(Stream stream) at ExcelDataReader.ExcelReaderFactory.CreateReader(Stream fileStream, ExcelReaderConfiguration configuration)

Below is part of the code where the error happens:
` Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

        Stream stream = await GetFTPFile(_ftp_UrlAndFilename, _ftp_username, _ftp_password);

        try
        {
            using var reader = ExcelReaderFactory.CreateReader(stream);

            DataSet ds = reader.AsDataSet(new ExcelDataSetConfiguration()
            {
                ConfigureDataTable = (tableReader) => new ExcelDataTableConfiguration()
                {
                    UseHeaderRow = true
                }
            });

            foreach (DataColumn column in ds.Tables[0].Columns)
            {
                if (column.ColumnName.Contains(""))
                    column.ColumnName = column.ColumnName.Replace(" ", "");
            }

            return ds.Tables[0].ToList<SellerCloudInventory>();
        }
        catch (Exception ex)
        {
            throw new Exception($"There was a problem converting the inventory file from an excel file. The stream length was {stream.Length}.", ex);
        }`

I'm pretty sure that the file we're reading is xlsx file. The error sometimes happens and sometimes don't.
Any help please? :) Thanks!

@appel1
Copy link
Collaborator

appel1 commented Nov 25, 2023

My guess would be that either the files are trunkated on the FTP source or not the whole file gets downloaded.

Does it happen if you download it to a local file first and then try to open it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants