Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

invalid entry size (expected 7486146 but got 7486147 bytes) error  RSS feed

 
Deepak Amar
Greenhorn
Posts: 11
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to parse data from a file of type .FTR which is inside a zip file placed at an FTP location. It has almost > 34000 lines.
The parser which is written takes input in the form of InputStream and parses the file using ZipInputStream.
Part of parser code:
public void parse(InputStream inputStream){
ZipInputStream zis = new ZipInputStream(inputStream);
ZipEntry entry = null;
while((entry = zis.getNextEntry()) != null)
{
reader = new BufferedReader(new InputStreamReader(zis));
parseData(reader );
}
}

There are two ways to input the file to the parser. Either to download that file from FTP, pass it to the Parser manually from the UI or there is code to download that from FTP and pass an InputStream to Parser.

Code to download from FTP site:
FTPClient ftp = new FTPClient();
ftp.connect(host);
ftp.login(user, pass);
FTPFile[] files = ftp.listFiles(file);
ByteArrayOutputStream outputStream = null;
for(int i=0;i<files.length;i++)
{
try
{
outputStream = new ByteArrayOutputStream();
ftp.retrieveFile(files[i].getName(), outputStream);
bytes = outputStream.toByteArray();
if (null == bytes || 0 == bytes.length)
continue;
parse(new ByteArrayInputStream(bytes));// call the parser's parse function to parse the data
}
catch(Exception e){
e.printStackTrace();
}
}



When i manually download the file from FTP and pass it to the parser from UI then it works.

But when i test it with the download code where i have used FTPClient to download the zip file then it gives invalid entry size (expected 7486146 but got 7486147 bytes).
FTPClient works for other types of file but it fails for zip

Please suggest.
I tried to google for this issue but still no progress.
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm... not sure where this topic belongs, but the ranch office is definitely not the correct forum for this.


Since there is mention of zip and ftp, let's try the other api forum first. Or maybe the streams forum is better ... not sure. Regardless, moved.

Henry
 
Deepak Amar
Greenhorn
Posts: 11
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys if anyone knows the solution for it then please suggest..
I have struggled a lot for this...
 
Maxim Karvonen
Ranch Hand
Posts: 121
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like a common problem with FTP.

Most likely you transfer your files as Ascii files (this is default mode for FTP). And zip files are definitely not ascii so they get corrupted at some stage. You should explicitly set file type to binary by calling FTPClient.setFileType(FTP.BINARY_FILE_TYPE) before any transfer (but after connecting and logging in).
 
Deepak Amar
Greenhorn
Posts: 11
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maxim Karvonen wrote:Looks like a common problem with FTP.

Most likely you transfer your files as Ascii files (this is default mode for FTP). And zip files are definitely not ascii so they get corrupted at some stage. You should explicitly set file type to binary by calling FTPClient.setFileType(FTP.BINARY_FILE_TYPE) before any transfer (but after connecting and logging in).


I had tried with Binary but it was giving error, so i removed it..
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Deepak Amar wrote:
I had tried with Binary but it was giving error, so i removed it..


For ZIP files, which is a binary file format, you definitely need to use the binary option. Basically, you tried the binary option, got an error, and created another error to mask the first error. Now, you have more errors -- albeit, not all of them will show up as compile errors.

Recommendation is to fix the binary issue, then tackle the error caused by that.

Henry
 
Deepak Amar
Greenhorn
Posts: 11
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Deepak Amar wrote:
I had tried with Binary but it was giving error, so i removed it..


For ZIP files, which is a binary file format, you definitely need to use the binary option. Basically, you tried the binary option, got an error, and created another error to mask the first error. Now, you have more errors -- albeit, not all of them will show up as compile errors.

Recommendation is to fix the binary issue, then tackle the error caused by that.

Henry


Thank you so much Maxim and Henry for your help and I really appreciate your prompt replies.
Actually i was setting binary file type after connecting and before logging into the FTP.
Thanks again Maxim for pointing out that..(setting of binary file type before connecting and logging in)
I was struggling to fix this since so long. It is finally resolved..

Thanks and Regards,
Deepak Amar
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Deepak Amar wrote:
Thanks again Maxim for pointing out that..(setting of binary file type before connecting and logging in)
I was struggling to fix this since so long. It is finally resolved..



And thanks for returning to inform us that is has been resolved. Have a cow. One for Maxim too.

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!