Java (Jdk+ant.jar package) way to implement zip packaging.

Source: Internet
Author: User
Package com.test;

Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;

Import Org.apache.tools.zip.ZipEntry;
Import Org.apache.tools.zip.ZipOutputStream;
/**
*
* Jdk+ant.jar package way to implement zip packaging.
* < features detailed description >
*
* @author Vincent
* @version [version number, 2014-10-10]
* @see [related classes/methods]
* @since [Product/module Version]
*/
public class Testzip
{
private void Zip (string soucefilename, String destfilename)
{
File File = new file (soucefilename);
Try
{
Zip (file, destFileName, "");
}
catch (IOException E)
{
E.printstacktrace ();
}
}

private void Zip (string soucefilename, String destfilename,string Base)
{
File File = new file (soucefilename);
Try
{
Zip (file, destfilename, base);
}
catch (IOException E)
{
E.printstacktrace ();
}
}

private void Zip (File soucefile, String destfilename,string Base)
Throws IOException
{
FileOutputStream fileout = null;
Try
{
Fileout = new FileOutputStream (destfilename);
}
catch (FileNotFoundException e)
{
E.printstacktrace ();
}
Zipoutputstream out = new Zipoutputstream (fileout);
Zip (soucefile, out, base);
Out.close ();
}

private void Zip (File soucefile, zipoutputstream out, String base)
Throws IOException
{

if (Soucefile.isdirectory ())
{
file[] files = soucefile.listfiles ();
Out.putnextentry (new ZipEntry (base + "/"));
Base = base.length () = = 0? "": Base + "/";
for (File file:files)
{
Zip (file, out, base + file.getname ());
}
}
Else
{
if (base.length () > 0)
{
Out.putnextentry (new ZipEntry (base));
}
Else
{
Out.putnextentry (New ZipEntry (Soucefile.getname ()));
}
System.out.println ("filepath=" + Soucefile.getpath ());
FileInputStream in = new FileInputStream (soucefile);

int b;
Byte[] by = new byte[1024];
while ((b = in.read (by))!=-1)
{
Out.write (by, 0, b);
}
In.close ();
}

}

/**
* ************************** more efficient way to zip package **************************************************************
*/
/**
* Another way to play a ZIP package------< features detailed description >
*
* @param epubzipdir--Source Path
* @param afterdrmpath---target path
* @return [parameter description]
*
* @return Long [return type description]
* @exception throws [type of offence] [illegal statement]
* @see [Class, Class # method, Class # member]
*/
Private Long Dozip (string epubzipdir, String afterdrmpath)
{
Long result = 0;
Zip z = new zip ();
File parent = new file (Epubzipdir);
Project P = new Project ();
Z.setproject (P);
Z.setbasedir (parent);
Z.setdestfile (New File (Afterdrmpath));
Z.execute ();
result = new File (afterdrmpath). Length ();
return result;
}
/**
* Test
*/
public static void Main (string[] args)
{
Testzip z = new Testzip ();
Z.zip ("C:/intel/extremegraphics", "C:/10.zip", "CNBJTW0/2014/08/21");
String filename= "CNBJTW003015109200000386141";
Z.zip ("f:/" +filename, "F:/zip_file1111.zip");
Z.zip ("f:/" +filename, "d:/zip_file88888.epub");
}

}

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.