This discussion is archived
2 Replies Latest reply: Feb 18, 2012 10:10 AM by mh*379860*ly RSS

Error when trying to unzip ZIP object

mh*379860*ly Newbie
Currently Being Moderated
HI Gentlemen:

I have got severe problems when trying to unzip my java object. Here is the program that reads a file from a German Health Care smart card correctly.
import opencard.core.service.SmartCard;
import opencard.core.service.CardRequest;
import opencard.opt.iso.fs.FileAccessCardService;
import opencard.opt.iso.fs.CardFile;

import java.io.*;     
import java.util.zip.*;

public class ReadFile {
public static void main(String[] args)
{

System.out.println("reading smartcard file...");
try {
SmartCard.start();
// wait for a smartcard with file access support
CardRequest cr = new CardRequest(CardRequest.NEWCARD, null, FileAccessCardService.class);
SmartCard sc = SmartCard.waitForCard(cr);
FileAccessCardService facs = (FileAccessCardService)sc.getCardService(FileAccessCardService.class, true);

CardFile root = new CardFile(facs);
CardFile df = new CardFile(facs, "#D27600000102");
CardFile file = new CardFile(df, ":D001");

byte[] data = facs.read(file.getPath(), 0, file.getLength() );
sc.close();

// Find first relevant byte backwards
System.out.println("data.length " + data.length);
int j=data.length;
while (j-- > 0 && data[j] <= 32)
  System.out.printf("data[%d]:  0x%X%n", j, data[j]);

// Shift out first two bytes
byte[] data1 = new byte[j+2];
System.arraycopy(data, 2, data1, 0, data1.length);
System.out.println("Shifted data -----------------------");
for (int i=0; i<data1.length; i++)
  System.out.printf("%d   0x%X%n", i, data1);


String entry = new String(data1);
System.out.println("entry.length(): " + entry.length());
byte[] u = entry.getBytes();
for (int i=0; i<u.length; i++)
System.out.printf("%d 0x%X%n", i,u[i]);

// Dump unzipped contents
ByteArrayInputStream bais = new java.io.ByteArrayInputStream(entry.getBytes());
GZIPInputStream zip = new java.util.zip.GZIPInputStream(bais);
Object fc = java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE, 10000);

byte[] fc1 = (byte[])fc;
int len = zip.read(fc1, 0, 10000);

System.out.println("Read from zip = " + len);
System.out.println(fc);

/* Stuff coming from javascript to be adapted yet
     ByteBuffer bb = new ByteBuffer(fc);
     var bs = bb.toByteString().left(len);
     print(bs.toString(UTF8));
*/


} catch (Exception e) {
e.printStackTrace(System.err);
} finally { // even in case of an error...
try {
SmartCard.shutdown();
} catch (Exception e) {
e.printStackTrace(System.err);
}
}
System.exit(0);
}
}
Output is redirected to a log file which follows:
reading smartcard file...
original raw data read from card ---------------
data.length 850
data[849]: 0x0
data[848]: 0x0
data[847]: 0x0
data[846]: 0x0
data[845]: 0x0
data[844]: 0x0
data[843]: 0x0
data[842]: 0x0
data[841]: 0x0
... abridged for you
data[400]: 0x0
data[399]: 0x0
data[398]: 0x3
Shifted data -----------------------
0 0x1F
1 0x8B
2 0x8
3 0x0
4 0x0
5 0x0
6 0x0
7 0x0
8 0x0
9 0x0
10 0x8D
11 0x52
12 0xCB
... again abridged
394 0x74
395 0x77
396 0x3
397 0x0
398 0x0
entry.length(): 399
entry ------------------------------
0 0x1F
1 0x8B
2 0x8
3 0x0
4 0x0
5 0x0
6 0x0
7 0x0
8 0x0
9 0x0
10 0x3F
11 0x52
... and again abridged
393 0xD2
394 0x74
395 0x77
396 0x3
397 0x0
398 0x0
There are 399 bytes in the card file 0..398, the first two of them must be cut off. This is done by shifting data two bytes to the left, pulls two extra null bytes on the right. This is what I exactly want. Now I would like to unzip the contents (note that the first two bytes 1F8B is a gzip magic number and must be there.) And I always get the following in a command window:
java.util.zip.ZipExcepstion: invalid block type
at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:164)
at java.util.zip.GZIPInputStream.read()GZIPInputStream.java:116)
at ReadFile.main(ReadFile.java:55)
Note that a corresponding javascript works well.
Does anyone have an idea where I am wrong?

Thanks, kind regards:

Miklos HERBOLY                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
  • 1. Re: Error when trying to unzip ZIP object
    EJP Guru
    Currently Being Moderated
    You're really doing this the hard way. Don't convert the data to a String and back again, that's where your problem is coming from. You don't need the System.arraycopy() either. Just provide the required offset when constructing the ByteArrayInputStream. That is:
    byte[] data = facs.read(file.getPath(), 0, file.getLength() );
    sc.close();
    
    // Find first relevant byte backwards
    System.out.println("data.length " + data.length);
    int j=data.length;
    while (j-- > 0 && data[j] <= 32)
      System.out.printf("data[%d]:  0x%X%n", j, data[j]);
    
    // Shift out first two bytes
    int offset = 2;
    ByteArrayInputStream bais = new java.io.ByteArrayInputStream(data, offset, j-offset);
    GZIPInputStream zip = new java.util.zip.GZIPInputStream(bais);
    Also I don't know why you are using Reflection to construct a new byte[10000].
  • 2. Re: Error when trying to unzip ZIP object
    mh*379860*ly Newbie
    Currently Being Moderated
    Hi EJP,

    many thanks for your reply. Now it has been working A1.

    Regards, Miklos

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points