Forum Stats

  • 3,752,658 Users
  • 2,250,533 Discussions
  • 7,867,907 Comments

Discussions

Error when trying to unzip ZIP object

mh*379860*ly
mh*379860*ly Member Posts: 287
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<em>);<br />
<br />
<br />
String entry = new String(data1);<br />
System.out.println("entry.length(): " + entry.length());<br />
byte[] u = entry.getBytes();<br />
for (int i=0; i<u.length; i++)<br />
System.out.printf("%d 0x%X%n", i,u[i]);<br />
<br />
// Dump unzipped contents<br />
ByteArrayInputStream bais = new java.io.ByteArrayInputStream(entry.getBytes());<br />
GZIPInputStream zip = new java.util.zip.GZIPInputStream(bais);<br />
Object fc = java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE, 10000);<br />
<br />
byte[] fc1 = (byte[])fc;<br />
int len = zip.read(fc1, 0, 10000);<br />
<br />
System.out.println("Read from zip = " + len);<br />
System.out.println(fc);<br />
<br />
/* Stuff coming from javascript to be adapted yet<br />
	ByteBuffer bb = new ByteBuffer(fc);<br />
	var bs = bb.toByteString().left(len);<br />
	print(bs.toString(UTF8));<br />
*/<br />
<br />
<br />
} catch (Exception e) {<br />
e.printStackTrace(System.err);<br />
} finally { // even in case of an error...<br />
try {<br />
SmartCard.shutdown();<br />
} catch (Exception e) {<br />
e.printStackTrace(System.err);<br />
}<br />
}<br />
System.exit(0);<br />
}<br />
}
<pre class="jive-pre"><code class="jive-code">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                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
Tagged:

Answers

  • EJP
    EJP Member Posts: 32,920 Gold Crown
    edited Feb 5, 2012 10:32PM
    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].
  • mh*379860*ly
    mh*379860*ly Member Posts: 287
    Hi EJP,

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

    Regards, Miklos
This discussion has been closed.