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");
}
}