Java compresses a single file

Source: Internet
Author: User


Import Java.io.BufferedInputStream;
Import Java.io.BufferedOutputStream;
Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.util.zip.ZipEntry;
Import Java.util.zip.ZipOutputStream;

/**
* Package files into zip compressed files
* @author JiangYang
* @since 2013-6-28
*/
Public final class Packfile {

Private Packfile () {

}

/**
* will be stored in the Sourcefilepath directory of the source files, packaged into the filename name of the zip file, and stored to Zipfilepath.
* @param sourcefilepath file path to be compressed
* @param zipfilepath after compression storage path
* @param filename Compressed file name
* @return Flag
*/
public static Boolean Filetozip (String sourcefilepath,string zipfilepath,string fileName) {
Boolean flag = false;
File SourceFile = new file (Sourcefilepath);
FileInputStream FIS = null;
Bufferedinputstream bis = null;
FileOutputStream fos = null;
Zipoutputstream Zos = null;

if (sourcefile.exists () = = False) {
System.out.println (">>>>>> to be compressed file directory: + Sourcefilepath +" does not exist. <<<<<< ");
} else {
try {
File ZipFile = new file (Zipfilepath + "/" + FileName + ". zip");
if (zipfile.exists ()) {
System.out.println (">>>>>>" + Zipfilepath +) directory exists under the name: "+ fileName +". zip "+" packaging files. <<<<<< ");
} else {
file[] SourceFiles = Sourcefile.listfiles ();
if (null = = SourceFiles | | Sourcefiles.length < 1) {
System.out.println (">>>>>> to be compressed file directory: + Sourcefilepath +" does not exist in the file, without compression. <<<<<< ");
} else {
FOS = new FileOutputStream (ZipFile);
Zos = new Zipoutputstream (new Bufferedoutputstream (FOS));
byte[] Bufs = new BYTE[1024*10];
for (int i=0;i<sourcefiles.length;i++) {
Create a zip entity and add a compressed package
ZipEntry zipentry = new ZipEntry (Sourcefiles[i].getname ());
Zos.putnextentry (ZipEntry);
Read the file to be compressed and write it into a compressed package
FIS = new FileInputStream (sourcefiles[i]);
bis = new Bufferedinputstream (FIS,1024*10);
int read = 0;
while ((Read=bis.read (bufs, 0, 1024*10))!=-1) {
Zos.write (bufs, 0, read);
}
}
Flag = true;
}
}
catch (FileNotFoundException e) {
E.printstacktrace ();
throw new RuntimeException (e);
catch (IOException e) {
E.printstacktrace ();
throw new RuntimeException (e);
finally {
Close the stream
try {
if (null!= bis) bis.close ();
if (null!= zos) zos.close ();
catch (IOException e) {
E.printstacktrace ();
throw new RuntimeException (e);
}
}
}

return flag;
}

/**
* Package files into Zip compressed file, Main method test
* @param args
*/
public static void Main (string[] args) {
String Sourcefilepath = "d:\\11";
String Zipfilepath = "d:\\";
String fileName = "Test";
Boolean flag = Packfile.filetozip (Sourcefilepath, Zipfilepath, fileName);
if (flag) {
System.out.println (">>>>>> file package successful. <<<<<< ");
} else {
System.out.println (">>>>>> file pack failed. <<<<<< ");
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.