Wednesday, 25 July 2018

Creating Zip File of Multiple Text File

This class will help you to create Zip File of List of Files.

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class DemoZip {

private ZipOutputStream zos;
public static void main(String[] args) throws IOException {
File file1 = new File("/home/pojke/ZipFile/a.txt");
File file2 = new File("/home/pojke/ZipFile/b.txt");
File file3 = new File("/home/pojke/ZipFile/c.txt");
ArrayList<File> files = new ArrayList<>();
files.add(file1);
files.add(file2);
files.add(file3);
DemoZip zip = new DemoZip();
zip.zipFile("/home/pojke/ZipFile/zipFile.zip", files);
}
private void zipFile(String outputZipFileName, ArrayList<File> files) throws IOException, IOException {
byte[] buffer = new byte[1024];
FileOutputStream fos = new FileOutputStream(outputZipFileName);
zos = new ZipOutputStream(fos);
files.stream().forEach(file ->{
try(
FileInputStream fis = new FileInputStream(file)) {
zos.putNextEntry(new ZipEntry(file.getName()));

int len;
while((len=fis.read(buffer))>0) {
zos.write(buffer,0,len);
}
zos.closeEntry();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
});
zos.close();
fos.close();

System.out.println("Zip File Created Successfully");
}

}

8 comments:

  1. Does your blog have a contact page? I’m having problems locating it but, I’d like to shoot you an email. I’ve got some recommendations for your blog you might be interested in hearing.
    AWS Training in Chennai |Best Amazon Web Services Training in Chennai
    AWS Training in Rajaji Nagar | Amazon Web Services Training in Rajaji Nagar

    Best AWS Training Institute in BTM Layout Bangalore ,AWS Coursesin BTM

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging.

    angularjs-Training in velachery

    angularjs-Training in annanagar

    angularjs Training in chennai

    angularjs Training in chennai

    ReplyDelete
  4. Thanks for splitting your comprehension with us. It’s really useful to me & I hope it helps the people who in need of this vital information. 
    python interview questions and answers | python tutorials

    ReplyDelete
  5. Very nice post here and thanks for it .I always like and such a super contents of these post.Excellent and very cool idea and great content of different kinds of the valuable information's.
    Devops Training courses
    Devops Training in Bangalore
    Best Devops Training in pune
    Devops interview questions and answers

    ReplyDelete
  6. This is most informative and also this post most user friendly and super navigation to all posts... Thank you so much for giving this information to me.. 

    best rpa training in chennai | rpa online training |
    rpa training in chennai |
    rpa training in bangalore
    rpa training in pune
    rpa training in marathahalli
    rpa training in btm

    ReplyDelete
  7. Really very nice blog information for this one and more technical skills are improve,i like that kind of post.
    Java training in Rajaji nagar | Java training in Kalyan nagar

    Java training in Kalyan nagar | Java training in Jaya nagar

    ReplyDelete