Java 创建包含文本文件的zip文件

Java 创建包含文本文件的zip文件,java,zip,Java,Zip,我已经试着解决这个问题一两天了,但似乎不知道如何准确地将文本文件添加到zip文件中,我能够想出如何将这些文本文件添加到7zip文件中,这非常简单,但zip文件在我看来由于某种原因要复杂得多。由于用户原因,我想返回一个zip文件 以下是我现在拥有的: 我知道代码目前还不太干净,我计划在完成基本功能后解决这个问题 private ZipOutputStream addThreadDumpsToZipFile(File file, List<Datapoint<ThreadDump>

我已经试着解决这个问题一两天了,但似乎不知道如何准确地将文本文件添加到zip文件中,我能够想出如何将这些文本文件添加到7zip文件中,这非常简单,但zip文件在我看来由于某种原因要复杂得多。由于用户原因,我想返回一个zip文件

以下是我现在拥有的: 我知道代码目前还不太干净,我计划在完成基本功能后解决这个问题

private ZipOutputStream addThreadDumpsToZipFile(File file, List<Datapoint<ThreadDump>> allThreadDumps, List<Datapoint<String>> allThreadDumpTextFiles) {
  ZipOutputStream threadDumpsZipFile = null;

  try {
      //creat new zip file which accepts input stream
      //TODO missing step: create text files containing each thread dump then add to zip
      threadDumpsZipFile = new ZipFile(new FileOutputStream(file));
      FileInputStream fileInputStream = null;
      try {
          //add data to each thread dump entry
          for(int i=0; i<allThreadDumpTextFiles.size();i++) {
              //create file for each thread dump
             File threadDumpFile = new File("thread_dump_"+i+".txt");
             FileUtils.writeStringToFile(threadDumpFile,allThreadDumpTextFiles.get(i).toString());

              //add entry/file to zip file (creates block to add input to)
              ZipEntry threadDumpEntry = new ZipEntry("thread_dump_"+i); //might need to add extension here?
              threadDumpsZipFile.putNextEntry(threadDumpEntry);

              //add the content to this entry
              fileInputStream = new FileInputStream(threadDumpFile);
              byte[] byteBuffer = new byte[(int) threadDumpFile.length()]; //see if this sufficiently returns length of data
              int bytesRead = -1;
              while ((bytesRead = fileInputStream.read(byteBuffer)) != -1) {
                  threadDumpsZipFile.write(byteBuffer, 0, bytesRead);
              }
          }
          threadDumpsZipFile.flush();
      } catch (FileNotFoundException e) {
          e.printStackTrace();
      } catch (IOException e) {
          e.printStackTrace();
      } finally {
          try {
              fileInputStream.close();
          } catch(Exception e) {

          }
      }
  } catch (FileNotFoundException e) {
      e.printStackTrace();
  } catch (IOException e) {
      e.printStackTrace();
  }
  return threadDumpsZipFile;
}

正如您所猜测的,我有一组线程转储,我想添加到我的zip文件并返回给用户

如果你们需要更多信息,请告诉我! PS:这个问题可能有一些bug,我刚刚通过一些断点意识到threadDumpFile.length不能真正工作

期待您的回复! 谢谢
阿尔萨

这里有一条裂缝。我认为在制作ZipEntry对象时,您应该保留文件扩展名。看看是否可以实现下面的createTextFiles功能;剩下的工作——我将该方法存根为返回一个test.txt文件,其中包含一些虚拟数据以进行验证

void zip()  
{ 
   try {
        FileOutputStream fos = new FileOutputStream("yourZipFile.zip");
        ZipOutputStream zos = new ZipOutputStream(fos);

        File[] textFiles = createTextFiles(); // should be an easy step

        for (int i = 0; i < files.length; i++) {
            addToZipFile(file[i].getName(), zos);
        }
        zos.close();
        fos.close();

    } catch (Exception e) {
        e.printStackTrace();
    } 
}

void addToZipFile(String fileName, ZipOutputStream zos) throws Exception {  
        File file = new File(fileName);
        FileInputStream fis = new FileInputStream(file);
        ZipEntry zipEntry = new ZipEntry(fileName);
        zos.putNextEntry(zipEntry);

        byte[] bytes = new byte[1024];
        int length;
        while ((length = fis.read(bytes)) >= 0) {
            zos.write(bytes, 0, length);
        }
        zos.closeEntry();
        fis.close();
    }