The Artima Developer Community
Sponsored Link

Java Buzz Forum
Java Tutorial : Java IO (Java File - How to compress files in ZIP format - Single file)

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Ram N

Posts: 2777
Nickname: ramram
Registered: Jul, 2014

Ram N is Java Programmer
Java Tutorial : Java IO (Java File - How to compress files in ZIP format - Single file) Posted: Nov 23, 2016 8:26 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Ram N.
Original Post: Java Tutorial : Java IO (Java File - How to compress files in ZIP format - Single file)
Feed Title: JAVA EE
Feed URL: http://ramj2ee.blogspot.com/feeds/posts/default?alt=rss
Feed Description: This blog has viedo tutorials and Sample codes related to below Technologies. 1.J2EE 2.Java 3.Spring 4.Hibernate 5.Database 6.Oracle 7.Mysql 8.Design Patterns
Latest Java Buzz Posts
Latest Java Buzz Posts by Ram N
Latest Posts From JAVA EE

Advertisement

Click here to watch in Youtube :
https://www.youtube.com/watch?v=P3GcnDeYtX8&list=UUhwKlOVR041tngjerWxVccw

Click the below Image to Enlarge
Java Tutorial : Java IO (Java File - How to compress files in ZIP format - Single file) 
Java Tutorial : Java IO (Java File - How to compress files in ZIP format - Single file) 
Java Tutorial : Java IO (Java File - How to compress files in ZIP format - Single file) 
 ZipDemo.java
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipDemo
{
public static void main(String[] args) throws IOException
{
ZipDemo zipDemo = new ZipDemo();
File file = new File("D:/work/HelloWorld.java");
zipDemo.zipFile("D:/work/java.zip", file);
}

private void zipFile(String outputZipFileName, File file)
throws IOException
{
byte[] buffer = new byte[1024];

/*
* All Streams will be closed automatically because they
* are within the "try-With-Resources" block.
*/

try (FileOutputStream fos = new FileOutputStream(outputZipFileName);
ZipOutputStream zos = new ZipOutputStream(fos);
FileInputStream fin = new FileInputStream(file))
{

ZipEntry ze = new ZipEntry(file.getName());
zos.putNextEntry(ze);

int len;
while ((len = fin.read(buffer)) > 0)
{
zos.write(buffer, 0, len);
}

/*
* Closes the current ZIP entry and positions
* the stream for writing the next entry.
*/

zos.closeEntry();
}

System.out.println("Zip file is created...");
}

}
Output
Zip file is created...

Refer: 
https://docs.oracle.com/javase/8/docs/api/index.html?java/util/zip/ZipOutputStream.html 
https://docs.oracle.com/javase/8/docs/api/java/util/zip/ZipEntry.html

Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/JavaIODemo_zip_single_file_App.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/JavaIODemo_zip_single_file_App

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/a3d05269db9922e504bb1f9de8d07031990c9910/BasicJava/JavaIODemo_zip_single_file_App/?at=master

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • Read: Java Tutorial : Java IO (Java File - How to compress files in ZIP format - Single file)

    Topic: Java Tutorial : Java IO (Java File - How to copy file) Previous Topic   Next Topic Topic: GitLab looks to transform app testing

    Sponsored Links



    Google
      Web Artima.com   

    Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use