有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java创建文本文件,对文件进行tar并将其放入inputstream?

我想将一个只包含一个字符串的文本文件放入tar存档,并将这个tar文件放入inputstream。。。我想做这个“在飞行”,而不保存任何临时文件到磁盘上

我尝试了这个片段,最后得到了一个文件:

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”),而是想把它直接放到inputstream中

我对所有这些inputstreams/outputstreams有点困惑,我不确定是否有可能在这些流中移动,而不是创建临时文件来执行此操作

THX&;溴


共 (2) 个答案

  1. # 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. # 2 楼答案

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

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

    相反