【问题标题】:Java code to zip file after write file completely完全写入文件后压缩文件的Java代码
【发布时间】:2013-09-01 01:19:49
【问题描述】:

我正在编写代码以创建 txt 文件,并且在完全写入该 txt 文件后意味着完全关闭该 txt 文件而不是压缩该文件。但是,我不知道为什么,它不会等到文件关闭之前文件关闭它压缩它.. 请帮帮我

这是我的代码:

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;


public class zipfile {

    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub

        BufferedWriter bfAllBWPownmanmainfest = null;
        String mainfest = "file\\" + "fileforzip" + ".txt";
        bfAllBWPownmanmainfest = new BufferedWriter(new FileWriter(mainfest));

        bfAllBWPownmanmainfest.write("jdshsdksdkhdshksd\n");
        bfAllBWPownmanmainfest.write("jdshsdksdkhsdfsdfsddshksd\n");
        bfAllBWPownmanmainfest.write("jdshsdksdsdfdskhdshksd\n");
        bfAllBWPownmanmainfest.write("jdshsdksddsfdskhdshksd\n");
        bfAllBWPownmanmainfest.write("jdshsdksddsfdskhdshksd\n");
        bfAllBWPownmanmainfest.write("jdshsdksdsdfdskhdshksd\n");
        bfAllBWPownmanmainfest.write("jdshsdksddsfsdkhdshksd\n");

        bfAllBWPownmanmainfest.flush();
        bfAllBWPownmanmainfest.close();

        //After close file than zip that!! please help me Thanks

        FileOutputStream fout = new FileOutputStream("test.zip");
        ZipOutputStream zout = new ZipOutputStream(fout);

        ZipEntry ze = new ZipEntry(mainfest);
        zout.putNextEntry(ze);
        zout.closeEntry();
        zout.close();

    }

}

关闭后 bfAllBWPownmanmainfest.close();比压缩它,我怎么能做到这一点,请帮助我提前谢谢!它创建空的zip文件,它没有等到文件完全关闭! 请帮我!!谢谢!!

【问题讨论】:

标签: java


【解决方案1】:

您创建了一个ZipEntry,但实际上并未将任何字节写入输出 zip 文件。您需要从文件的InputStream 读取并在putNextEntry(ZipEntry) 之后写入您的ZipOutputStream

FileInputStream in = new FileInputStream(mainfest);
byte[] bytes = new byte[1024];
int count;

FileOutputStream fout = new FileOutputStream("test.zip");
ZipOutputStream zout = new ZipOutputStream(fout);

ZipEntry ze = new ZipEntry(mainfest); // this is the name as it will appear if you opened the zip file with WinZip or some other zip manager
zout.putNextEntry(ze);

while ((count = in.read(bytes)) > 0) {
    zout.write(bytes, 0, count);
}

zout.closeEntry();
zout.close();

【讨论】:

  • 感谢您的帮助!!但是当您打开 zip 文件时,它也会显示文件路径,有什么方法可以删除它,因为当我将其他文件压缩到该 zip 中时,它做错了!请帮帮我!!
  • 感谢您的帮助!!但是当您打开 zip 文件时,它也会显示文件路径,有什么方法可以删除它,因为当我将其他文件压缩到该 zip 中时,它做错了!请帮我!!当我在其中压缩其他文件时,它会创建所有文件夹而不是压缩文件!
  • @user2726811 将您传递的字段更改为ZipEntry。让它只是文件名(无论你想要什么)。
【解决方案2】:

您不需要ZipEntry。在bfAllBWPownmanmainfest.close(); satement 之前尝试此代码。

    ZipOutputStream zout = new ZipOutputStream(fout);
    int size = 0;
    byte[] b = new byte[1000];
    while ((size = bfAllBWPownmanmainfest.read(b)) > 0) {
       zout.write(b, 0, size);
    }
    zout.close();
    bfAllBWPownmanmainfest.close();

【讨论】:

    猜你喜欢
    • 2011-07-04
    • 1970-01-01
    • 1970-01-01
    • 2016-10-09
    • 1970-01-01
    • 1970-01-01
    • 2011-05-17
    • 1970-01-01
    • 2011-11-30
    相关资源
    最近更新 更多