【问题标题】:Create text file, tar the file and put it into an inputstream?创建文本文件,压缩文件并将其放入输入流?
【发布时间】:2016-02-20 19:17:10
【问题描述】:

我想将仅包含一个字符串的文本文件放入 tar 存档中,并将此 tar 文件放入输入流中...我想“即时”执行此操作,而不将任何临时文件保存到磁盘...

我尝试了这个 sn-p,最终得到了一个文件:

FileOutputStream fos = null;
    try {
        fos = new FileOutputStream ("helloworld.txt");
    } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } 

    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    String config = "HelloWorld!";
    byte[] b = config.getBytes();
    try {
        baos.write(b);
        baos.writeTo(fos);
        fos.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

我查看了 jtar 库: https://code.google.com/p/jtar/

所以我想将该文本文件直接放入该 tar 中,但我也不需要新的 FileOutputStream("c:/test/test.tar"),而是将其直接放入输入流中。

我对所有这些输入流/输出流有点困惑,我不确定是否可以移动一堆这些流而不是创建临时文件来执行此操作。

THX & BR

【问题讨论】:

  • 输入流代表什么?是过程输入吗?
  • 我将它发送(HTTP PUT)到一个 REST API,它接收它并期望一个 tar 流。

标签: java inputstream tar fileoutputstream


【解决方案1】:

我不确定这是否是最好的方法,但这似乎有效,但并不完全像我想象的那样:

        File tempFile = File.createTempFile("config", ".properties");

    BufferedWriter bw = new BufferedWriter(new FileWriter(tempFile));
    bw.write("Helloworld!!!!");
    bw.close();

    File tempTarFile = File.createTempFile("config", ".tar");

    FileOutputStream dest = new FileOutputStream( tempTarFile );
    TarOutputStream out = new TarOutputStream( new BufferedOutputStream( dest ) );
    out.putNextEntry(new TarEntry(tempFile, "config.properties"));
    BufferedInputStream origin = new BufferedInputStream(new FileInputStream( tempFile ));

    int count;
    byte data[] = new byte[2048];
    while((count = origin.read(data)) != -1) {
       out.write(data, 0, count);
    }
    out.flush();
    origin.close();
    out.close();

    tempFile.delete();
    tempTarFile.delete();

    InputStream inputStream = new FileInputStream(tempTarFile);

//to check if it worked:

//      OutputStream outputStream = null;
//
//      try {
//
//          // write the inputStream to a FileOutputStream
//          outputStream = 
//                        new FileOutputStream(new File("config.tar"));
//
//          int read = 0;
//          byte[] bytes = new byte[1024];
//
//          while ((read = inputStream.read(bytes)) != -1) {
//              outputStream.write(bytes, 0, read);
//          }
//
//          System.out.println("Done!");
//
//      } catch (IOException e) {
//          e.printStackTrace();
//      } finally {
//          if (inputStream != null) {
//              try {
//                  inputStream.close();
//              } catch (IOException e) {
//                  e.printStackTrace();
//              }
//          }
//          if (outputStream != null) {
//              try {
//                  // outputStream.flush();
//                  outputStream.close();
//              } catch (IOException e) {
//                  e.printStackTrace();
//              }
//
//          }
//      }

【讨论】:

    【解决方案2】:

    去掉FileOutputStreambos.writeTo(),然后做:

    ByteArrayInputStream bais = new ByteArrayInputStream (baos.toByteArray());
    

    改为。

    【讨论】:

      猜你喜欢
      • 2022-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-29
      • 1970-01-01
      • 2017-07-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多