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!

2
  • Just an fyi, close flushes the buffer so calling flush then close is redundant. Commented Aug 28, 2013 at 19:45
  • Refer to this stackoverflow.com/questions/1091788/… You have to write to ZipOutputStream . Commented Aug 28, 2013 at 19:55

2 Answers 2

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();
Sign up to request clarification or add additional context in comments.

3 Comments

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!!
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 Change the field you pass to ZipEntry. Make it be just the file name (whatever you want).
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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.