java.io.IOException:传入ByteArrayInputStream时不是GZIP格式

时间:2012-09-07 16:00:54

标签: java jaxb gzipinputstream

        final ByteArrayOutputStream stream = new ByteArrayOutputStream();
        try {
            jaxbContext.createMarshaller().marshal(proposal, stream);
            try {
                    stream.close();
            } catch (IOException e) {
                throw new PersistenceException("Failed to close output stream for " + proposal.getUniqueId());
            }
            // call persistenceService.put to send data to respective PersistenceServiceClass
            InputStream inputStream = new ByteArrayInputStream(stream.toByteArray());
            persistenceService.put(inputStream, proposal.getUniqueId());

persistenceService.put看起来像

public void put(@Nonnull final InputStream inputStream, @Nonnull final String uniqueId) throws PersistenceException {
        // a.) create xml.zip of inputStream as inputStream
        final GZIPInputStream zipInputStream;
        try {
            zipInputStream = new GZIPInputStream(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
            throw new PersistenceException("Persistence Service could not received input stream to persist for " + uniqueId);
        }

        // b.) call amazonS3client.putObject() to put data
        final PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, getProposalName(uniqueId), zipInputStream, new ObjectMetadata());
    }

当我运行此代码时,我看到以下stacktrace

java.io.IOException: Not in GZIP format
    at java.util.zip.GZIPInputStream.readHeader(GZIPInputStream.java:141)
    at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:56)
    at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:65)
    at com.sunrunhome.sparrow.business.xml.persist.S3Service.put(S3Service.java:71)
    at com.sunrunhome.sparrow.business.ProposalManager.putDocument(ProposalManager.java:56)
    at com.sunrunhome.sparrow.business.ProposalManagerTest.testWriteToS3(ProposalManagerTest.java:165)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.rules.ExternalResource$1.evaluate(ExternalResource.java:46)
    at org.junit.rules.ExternalResource$1.evaluate(ExternalResource.java:46)
    at org.junit.rules.RunRules.evaluate(RunRules.java:18)

我在这里做的不是什么?

1 个答案:

答案 0 :(得分:0)

private static void gzip(File file) {
    //logic to extract file
    GZIPInputStream in = null;
    FileInputStream fin = null;
    OutputStream out = null;
    int noread = 0;

    try {
        in = new GZIPInputStream(new FileInputStream(file));
        extractedFile = new File("C:\\DATA\\extracted.txt");
        out = new FileOutputStream(extractedFile);
        byte[] buffer = new byte[65536];
        noread = in.read(buffer);

        while(noread > 0) {
            out.write(buffer, 0, noread);
            System.out.println("done");
            break;
        }
    }

您可以将其作为fileInputStream进行概要传递并进行测试