3

I am writing some code to create a txt file and after completely writing in that txt file, completely close the txt file and then zip that file. But, I don't why, it does not wait until file close before file close it zip it..

Here is my code:

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();
        
    }

}

After close bfAllBWPownmanmainfest.close(); then zip it, how can I do that. It creates empty zip file, it didn't wait until file close completely!

vvvvv
  • 25,404
  • 19
  • 49
  • 81
user2726811
  • 151
  • 1
  • 2
  • 8
  • Just an fyi, close flushes the buffer so calling flush then close is redundant. – Grammin Aug 28 '13 at 19:45
  • Refer to this http://stackoverflow.com/questions/1091788/how-to-create-a-zip-file-in-java You have to write to ZipOutputStream . – sri Aug 28 '13 at 19:55

2 Answers2

3

You've created a ZipEntry, but haven't actually written any bytes to the output zip file. You need to read from an InputStream of your file and write to your ZipOutputStream after you've putNextEntry(ZipEntry).

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();
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • thanks for your help!! but when you open the zip file it says the file path as well, is there any way that, we can remove that, because when i zip other files in that zip, its doing wrong!! Please help me!! – user2726811 Aug 28 '13 at 20:24
  • thanks for your help!! but when you open the zip file it says the file path as well, is there any way that, we can remove that, because when i zip other files in that zip, its doing wrong!! Please help me!! when i zip other files in it, it create all the folder and than zip file!! – user2726811 Aug 28 '13 at 20:27
  • @user2726811 Change the field you pass to `ZipEntry`. Make it be just the file name (whatever you want). – Sotirios Delimanolis Aug 28 '13 at 20:30
0

You don not require ZipEntry.Try this code before 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();
Prabhaker A
  • 8,317
  • 1
  • 18
  • 24