如何正确地将zip文件读入字符串?

yk9xbfzb  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(386)

我遇到了一个问题,当试图读取压缩文件字符串。我的目标是在不解压缩的情况下将文件读取为字符串。
我希望我的文件看起来像这样:在记事本中打开的正确文件的屏幕截图
但是,当我使用此方法读取文件并将此字符串保存到文件时:

String content = "";
        try {
            content = new String(Files.readAllBytes(Paths.get(file2.getAbsolutePath())), "Windows-1250");
        } catch (IOException e) {
            e.printStackTrace();
        }

最后我看到的文件是这样的:错误文件的截图
这两个是如此相似,但不同-第二个是不是一个有效的zip存档截图的消息
我在想,也许这是错误的字符编码的情况-但我已经尝试了这么多,他们似乎没有工作-windows-1250看起来最正确的。
我还是一个初学者,所以如果我错过了一些重要的东西,我不会感到惊讶。请帮帮我!

koaltpgm

koaltpgm1#

我已经改变了我的方法来解决这个问题,而不是把这个文件作为内容发送,我把它作为附件发送。这是对我有效的代码:

MessageFactory messageFactory = MessageFactory.newInstance();
    SOAPMessage soapMessage = messageFactory.createMessage();
    SOAPPart soapPart = soapMessage.getSOAPPart();

    //Authenticate
    String authString = "LOGIN" + ":" + "PASSWORD";
    String enc = java.util.Base64.getEncoder().encodeToString((authString).getBytes(StandardCharsets.UTF_8));
    MimeHeaders hd = soapMessage.getMimeHeaders();
    hd.addHeader("Authorization", "Basic " + enc);

    // SOAP Header
    MimeHeaders headers = soapMessage.getMimeHeaders();
    headers.addHeader("SOAPAction", "/fscmService/ErpIntegrationService");

    // SOAP Envelope
    SOAPEnvelope envelope = soapPart.getEnvelope();
    envelope.addNamespaceDeclaration("erp", "http://xmlns.oracle.com/apps/financials/commonModules/shared/model/erpIntegrationService/");
    envelope.addNamespaceDeclaration("typ", "http://xmlns.oracle.com/apps/financials/commonModules/shared/model/erpIntegrationService/types/");

    // SOAP Body
    SOAPBody soapBody = envelope.getBody();
    .
    .
    .
    CREATING MY SOAP BODY
    .
    .
    .

    AttachmentPart attachment = soapMessage.createAttachmentPart();

    File file = new File(MYFILE.ZIPDIRECTORY);

    InputStream targetStream = new FileInputStream(file);

    attachment.setRawContent(targetStream, Files.probeContentType(file.toPath()));

    attachment.setContentId(fileName);

    soapMessage.addAttachmentPart(attachment);

    soapMessage.saveChanges();

这样我可以正确地发送我的文件。

相关问题