相关疑难解决方法(0)

在byte []中创建内存中的zip文件.Zip文件总是被破坏了

我的创建的zip文件有问题.我正在使用Java 7.我尝试用字节数组创建一个zip文件,其中包含两个或多个Excel文件.应用程序完成,无任何例外.所以,我认为一切都很好.在我尝试打开zip文件后,Windows 7出现了一条错误消息,即zip文件可能已损坏.我无法打开它,我不知道为什么......!我搜索了这个问题,但我发现的代码片段看起来与我的实现完全相同.

这是我的代码:

if (repsList.size() > 1)
{
  String today = DateUtilities.convertDateToString(new Date(), "dd_MM_yyyy");
  String prefix = "recs_" + today;
  String suffix = ".zip";
  ByteArrayOutputStream baos = null;
  ZipOutputStream zos = null;
  try
  {
    baos = new ByteArrayOutputStream();
    zos = new ZipOutputStream(baos);

    for (RepBean rep : repsList)
    {
      String filename = rep.getFilename();
      ZipEntry entry = new ZipEntry(filename);
      entry.setSize(rep.getContent().length);
      zos.putNextEntry(entry);
      zos.write(rep.getContent());
      zos.closeEntry();
    }
    // this is the zip file as byte[]
    reportContent = baos.toByteArray();

  }
  catch (UnsupportedEncodingException e)
  {
    ... …
Run Code Online (Sandbox Code Playgroud)

java zip

6
推荐指数
1
解决办法
6155
查看次数

将文件添加到ZIP文件

我试图将一些文件添加到ZIP文件,它创建文件但不添加任何内容.代码1:

String fulldate = year + "-" + month + "-" + day + "-" + min;

File dateFolder = new File("F:\\" + compname + "\\" + fulldate);
dateFolder.mkdir();

String zipName = "F:\\" + compname + "\\" + fulldate + "\\" + fulldate + ".zip";

zipFolder(tobackup, zipName);
Run Code Online (Sandbox Code Playgroud)

我的功能:

public static void zipFolder(File folder, String name) throws Exception {
    byte[] buffer = new byte[18024];

    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(name));
    FileInputStream in = new FileInputStream(folder);

    out.putNextEntry(new ZipEntry(name));

    int len;

    while((len = …
Run Code Online (Sandbox Code Playgroud)

java directory zip fileinputstream zipoutputstream

1
推荐指数
1
解决办法
2万
查看次数

标签 统计

java ×2

zip ×2

directory ×1

fileinputstream ×1

zipoutputstream ×1