• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Is there a way to recognize if the data is compressed or not?

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do you differentiate between the zipped data and unzipped data before applying the compression APIs as they will throw an exception if you try to unzip the non-zip data?
Is there a way to recognize if the data is compressed or not?
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The easiest ways it probably to catch the exception, pretend it never happened and treat the file as an unzipped file.
 
Siddharth bal sharma
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
easiest but not the correct way. shouldn't catch exceptions to terminate a condition. I have both formats of data- zipped and unzipped that need to be processed(in the unzipping scenario).I can catch the exception while unzipping and assume that data is not compressed and do the normal processing but this is not the correct way. I should first gauge that the data is in zip format or not and then do the processing. try/catch blocks are anyways heavy.
 
Joe Ess
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Siddharth bal sharma:
easiest but not the correct way.



That's rather strong. If it works, how can it not be "correct"?
I agree that under normal circumstances, using exceptions for testing is a Bad Thing (I loathe that ObjectInputStream/ObjectOutputStream signals EOF with an exception). However, you need some functionality. Someone has already implemented that functionality in the API. Does it really matter that you have to break convention to use it? I don't think so. If it does, then maybe someone else has a better idea (I don't).
As for performance, unless your app is churning through hundreds of files and users are waiting for them, I don't think it matters that it takes a few milliseconds to set up the try block in this case.
 
Siddharth bal sharma
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
lottsa retrofit required to make this try/catch in my case.
Does anyone know what is the header for a zip format. If we can check the header for a datastream- may be we have a chance to detmine!
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The first two characters of a zipfile are PK (0x50, 0x4B). Opening the file, reading the two characters, checking their identity is, indeed, going to be more efficient than the try/fail technique. But I agree with Joe that it's false economy to worry about it. The try/catch version is going to be simpler and more obvious; the character-checking version will be more complex and need an explanatory comment. I'd do it the simpler way unless and until it's shown to be a problem.
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Surely starting with "PK" is only a necessary, not a sufficient condition for checking for a ZIPped file. After all, a non-zipped file might happen to start with "PK" too. It's only two characters, so it's really not all that unlikely.

So, even if you check for "PK", you still need the try...catch as well.
 
Siddharth bal sharma
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well..i need to handle both zip format and unzip format of data in the same piece of code, so a statement like

try
{
process zip data
}(catch ZipException)
{
process unzip data
}

is not neat .
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Something like

Should do what you want, right?
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic