Beispiel #1
0
        /**
         * Create an archive input stream from an input stream, autodetecting
         * the archive type from the first few bytes of the stream. The InputStream
         * must support marks, like BufferedInputStream.
         *
         * @param in the input stream
         * @return the archive input stream
         * @throws ArchiveException if the archiver name is not known
         * @throws IllegalArgumentException if the stream is null or does not support mark
         */
        public ArchiveInputStream createArchiveInputStream(java.io.InputStream inJ)
        //throws ArchiveException
        {
            if (inJ == null)
            {
                throw new java.lang.IllegalArgumentException("Stream must not be null.");
            }

            if (!inJ.markSupported())
            {
                throw new java.lang.IllegalArgumentException("Mark is not supported.");
            }

            byte[] signature = new byte[12];
            inJ.mark(signature.Length);
            try {
                int signatureLength = inJ.read(signature);
                inJ.reset();
                if (ZipArchiveInputStream.matches(signature, signatureLength))
                {
                    return(new ZipArchiveInputStream(inJ));
                }
                else if (JarArchiveInputStream.matches(signature, signatureLength))
                {
                    return(new JarArchiveInputStream(inJ));
                }
                else if (ArArchiveInputStream.matches(signature, signatureLength))
                {
                    return(new ArArchiveInputStream(inJ));
                }
                else if (CpioArchiveInputStream.matches(signature, signatureLength))
                {
                    return(new CpioArchiveInputStream(inJ));
                }
                // Tar needs a bigger buffer to check the signature; read the first block
                byte[] tarheader = new byte[512];
                inJ.mark(tarheader.Length);
                signatureLength = inJ.read(tarheader);
                inJ.reset();
                if (TarArchiveInputStream.matches(tarheader, signatureLength))
                {
                    return(new TarArchiveInputStream(inJ));
                }
            } catch (java.io.IOException e) {
                throw new ArchiveException("Could not use reset and mark operations.", e);
            }

            throw new ArchiveException("No Archiver found for the stream signature");
        }
Beispiel #2
0
        /**
         * Read informations of a rpm file out of an input stream.
         *
         * @param rpmInputStream The input stream representing the rpm file
         * @throws IOException if an error occurs during read of the rpm file
         */
        private void readFromStream(java.io.InputStream rpmInputStream)
        {//throws IOException {
            ByteCountInputStream allCountInputStream = new ByteCountInputStream(
                rpmInputStream);

            java.io.InputStream inputStream = new java.io.DataInputStream(allCountInputStream);

            lead      = new RPMLead((java.io.DataInputStream)inputStream);
            signature = new RPMSignature((java.io.DataInputStream)inputStream, store);

            if (logger.isLoggable(java.util.logging.Level.FINER))
            {
                logger.finer("Signature Size: " + signature.getSize());
            }

            header = new RPMHeader((java.io.DataInputStream)inputStream, store);

            if (logger.isLoggable(java.util.logging.Level.FINER))
            {
                logger.finer("Header Size: " + header.getSize());
            }

            DataTypeIf payloadTag    = getTag("PAYLOADFORMAT");
            String     payloadFormat = payloadTag != null?payloadTag.toString()
                                           : "cpio";

            DataTypeIf payloadCompressionTag = getTag("PAYLOADCOMPRESSOR");
            String     payloadCompressor     = payloadCompressionTag != null?payloadCompressionTag
                                               .toString()
                                                   : "gzip";

            if (payloadFormat.equals("cpio"))
            {
                if (logger.isLoggable(java.util.logging.Level.FINER))
                {
                    logger.finer("PAYLOADCOMPRESSOR: " + payloadCompressor);
                }

                if (payloadCompressor.equals("gzip"))
                {
                    inputStream = new GzipCompressorInputStream(allCountInputStream);
                }
                else if (payloadCompressor.equals("bzip2"))
                {
                    inputStream = new BZip2CompressorInputStream(allCountInputStream);
                }
                else if (payloadCompressor.equals("lzma"))
                {
                    try
                    {
                        java.io.PipedOutputStream pout = new java.io.PipedOutputStream();
                        inputStream = new java.io.PipedInputStream(pout);
                        byte[] properties = new byte[5];
                        if (allCountInputStream.read(properties, 0, 5) != 5)
                        {
                            throw (new java.io.IOException("input .lzma is too short"));
                        }
                        SevenZip.Compression.LZMA.Decoder decoder = new SevenZip.Compression.LZMA.Decoder();
                        decoder.SetDecoderProperties(properties);
                        long outSize = 0;
                        for (int i = 0; i < 8; i++)
                        {
                            int v = allCountInputStream.read();
                            if (v < 0)
                            {
                                throw (new java.io.IOException("lzma error : Can't Read 1"));
                            }
                            outSize |= ((long)v) << (8 * i);
                        }
                        if (outSize == -1)
                        {
                            outSize = java.lang.Long.MAX_VALUE;
                        }

                        Decode decoderRunnable = new Decode(decoder,
                                                            allCountInputStream, pout, outSize);
                        java.lang.Thread t = new java.lang.Thread(decoderRunnable, "LZMA Decoder");
                        t.start();
                    }
                    catch (java.lang.NoClassDefFoundError)
                    {
                        String message = "No LZMA library found. Attach p7zip library to classpath (http://p7zip.sourceforge.net/)";
                        logger.severe(message);
                        throw new java.io.IOException(message);
                    }
                }
                else if (payloadCompressor.equals("none"))
                {
                    inputStream = allCountInputStream;
                }
                else
                {
                    throw new java.io.IOException("Unsupported compressor type "
                                                  + payloadCompressor);
                }

                ByteCountInputStream    countInputStream = new ByteCountInputStream(inputStream);
                CpioArchiveInputStream  cpioInputStream  = new CpioArchiveInputStream(countInputStream);
                CpioArchiveEntry        readEntry;
                java.util.List <String> fileNamesList = new java.util.ArrayList <String>();
                String fileEntry;
                while ((readEntry = cpioInputStream.getNextCPIOEntry()) != null)
                {
                    if (logger.isLoggable(java.util.logging.Level.FINER))
                    {
                        logger.finer("Read CPIO entry: " + readEntry.getName()
                                     + " ;mode:" + readEntry.getMode());
                    }
                    if (readEntry.isRegularFile() || readEntry.isSymbolicLink() ||
                        readEntry.isDirectory())
                    {
                        fileEntry = readEntry.getName();
                        if (fileEntry.startsWith("./"))
                        {
                            fileEntry = fileEntry.substring(1);
                        }
                        fileNamesList.add(fileEntry);
                    }
                }
                store.setTag("FILENAMES", TypeFactory.createSTRING_ARRAY((String[])fileNamesList.toArray(new String[fileNamesList.size()])));

                setHeaderTagFromSignature("ARCHIVESIZE", "PAYLOADSIZE");
                // check ARCHIVESIZE with countInputStream.getCount();
                Object archiveSizeObject = getTag("ARCHIVESIZE");
                if (archiveSizeObject != null)
                {
                    if (archiveSizeObject is INT32)
                    {
                        int archiveSize = ((INT32)archiveSizeObject).getData()[0];
                        if (archiveSize != countInputStream.getCount())
                        {
                            new java.io.IOException("ARCHIVESIZE not correct");
                        }
                    }
                }
                store.setTag("J_ARCHIVESIZE", TypeFactory
                             .createINT64(new long[] { countInputStream.getCount() }));
            }
            else
            {
                throw new java.io.IOException("Unsupported Payload type " + payloadFormat);
            }

            // filling in signatures
            // TODO: check signatures!
            setHeaderTagFromSignature("SIGSIZE", "SIZE");
            setHeaderTagFromSignature("SIGLEMD5_1", "LEMD5_1");
            setHeaderTagFromSignature("SIGPGP", "PGP");
            setHeaderTagFromSignature("SIGLEMD5_2", "LEMD5_2");
            setHeaderTagFromSignature("SIGMD5", "MD5");
            setHeaderTagFromSignature("SIGGPG", "GPG");
            setHeaderTagFromSignature("SIGPGP5", "PGP5");
            setHeaderTagFromSignature("DSAHEADER", "DSA");
            setHeaderTagFromSignature("RSAHEADER", "RSA");
            setHeaderTagFromSignature("SHA1HEADER", "SHA1");

            store.setTag("J_FILESIZE", TypeFactory
                         .createINT64(new long[] { allCountInputStream.getCount() }));

            rpmInputStream.close();
        }