I have a task to create an app that scans a 2D code (DataMatrix) with a usb scanner (set to work as a serial port), then creat a zip file from the inputstream, unzip it and get an xml.
i have managed to scan the code, get the inputstream but i can't unzip it from java. I can open the zip file from explorer, and see a file called "array". If i open it as an xml i can see the info i want but also some bogus characters at the begining and end of file
Here's how the file looks inside: ˙˙˙˙ g<P SC="ABCDE" SN="1000000001" PS="111144" CN="2012" CC="1417856" OU="CAS-B" ID="2012-05-04" CT="CLN" />
here is the dataAvailable event
protected void dataAvailable(SerialPortEvent event)
{
try
{
if (is.available() > 0)
{
int numBytes = is.available();
readBufferArray = new byte[numBytes];
int readBytes = is.read(readBufferArray);
String one = new String(readBufferArray);
System.out.println("readBytes " + one);
}
FileOutputStream out = new FileOutputStream(winFile, true);
out.write(readBufferArray);
out.close();
String xml = unzipFile(winFile);
}
catch(Exception exc)
{
exc.printStackTrace();
}
}
Now if I try to exctract the xml from the zip file using this method:
public static String unzipFile(File fileName)
{
try
{
String destinationname = "d:\\temp\\";
String outPath = "";
byte[] buf = new byte[1024];
ZipInputStream zipinputstream = null;
ZipEntry zipentry;
zipinputstream = new ZipInputStream(
new FileInputStream(fileName.getPath()));
zipentry = zipinputstream.getNextEntry();
while (zipentry != null)
{
//for each entry to be extracted
String entryName = zipentry.getName();
System.out.println("entryname "+entryName);
int n;
FileOutputStream fileoutputstream;
File newFile = new File(entryName);
String directory = newFile.getParent();
if(directory == null)
{
if(newFile.isDirectory())
break;
}
outPath = destinationname+entryName;
fileoutputstream = new FileOutputStream(
destinationname+entryName);
while ((n = zipinputstream.read(buf, 0, 1024)) > -1)
fileoutputstream.write(buf, 0, n);
fileoutputstream.close();
zipinputstream.closeEntry();
zipentry = zipinputstream.getNextEntry();
}//while
zipinputstream.close();
return outPath;
}
catch(Exception exc)
{
exc.printStackTrace();
return "";
}
}
I get an exception at while ((n = zipinputstream.read(buf, 0, 1024)) > -1) that says java.util.zip.ZipException: invalid entry size (expected 4294967295 but got 127 bytes)
. The file "array" inside the zip is indeed 127 bytes in size. But I don't know what else to do.
What am I missing? I tried different 2D codes with different files. But they all turn out with bogus characters inside (before and after the xml tags).
Thanks