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

Recommended Answers

All 2 Replies

Can you make a small program that compile, executes and shows the problem? Is the serial port required for testing, or can the testing be done without it?
Does the dataAvailable() method write a good file?

so i have managed to resolve the java.util.zip.ZipException: invalid entry size exception. Instead of reading the inputstream into a new file then open it as a zip file i have read the input stream directly into a zipoutputstream. That creates a correctly configured zip file and i can extract it from java.

The only problem remains that the barcode is encoded with base256 algorithm and now i can't get a readable output.

        protected void dataAvailable(SerialPortEvent event)
        {
            try
            {
                byte[] decoded;
                if (is.available() > 0) 
                {
                    ZipOutputStream outStream = new ZipOutputStream(new FileOutputStream(winFile));
                    outStream.putNextEntry(new ZipEntry("array"));
                    byte[] buffer = new byte[1024];
                    int bytesRead;
                    while ((bytesRead = is.read(buffer)) > 0) {
                        outStream.write(buffer, 0, bytesRead);
                    }

                    outStream.closeEntry();

                    outStream.close();
                    is.close();   
                    String xml = unzipFile(winFile);
                }                
            }
            catch(Exception exc)
            {
                exc.printStackTrace();
            }
        }

The program is ok... i get the output file, only problem is how do i decode the base256 into utf-8 (my guess) so it creates the zipoutputstream with the xml file inside. (because now it does create it but the xml is encoded to something like PK- Iw¤@\Ś<˙˙˙˙˙˙˙˙ array e -‹= (first part of file only)

As I now the zip file must begin with "PK" and as I see the file inside the zip is called array. This is why I created the ZipEntry called "array". But it puts all the input stream in the file inside the zip. Which I don't know if it's ok.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.