So I was bored last night and thought I should write a code that creates a file and puts it into a zip file. So its very simple and here is the code in Java:
import java.io.*;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class WriteZip {
public static void main(String[] args) throws IOException {
FileOutputStream fout = new FileOutputStream("file.zip");
ZipOutputStream zout = new ZipOutputStream(fout);
ZipEntry ze = new ZipEntry("test.doc");
zout.putNextEntry(ze);
zout.closeEntry();
zout.close();
}
}
- So what happens here is that we create a FileOutputStream object which i called fout. This is the object that will create our file.zip.
- The next step is to create a ZipOutputStream which is an object that takes our FileOutputStream object called fout.
- Now we create a zip entry which is a new file called test.doc
- We then put this zip entry into our zipoutput stream,
- Lastly we close our zout
Happy coding everyone! Lookout for more cool java codes
0 comments:
Post a Comment