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包方式实现zip打包。
* <功能详细描述>
*
* @author Vincent
* @version [版本号, 2014-10-10]
* @see [相关类/方法]
* @since [产品/模块版本]
*/
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();
}
* 另一种方式打zip包------22 <功能详细描述>
*
* @param epubZipDir--源路径
* @param afterDrmPath ---目标路径
* @return [参数说明]
*
* @return long [返回类型说明]
* @exception throws [违例类型] [违例说明]
* @see [类、类#方法、类#成员]
*/
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");
}
}
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包方式实现zip打包。
* <功能详细描述>
*
* @author Vincent
* @version [版本号, 2014-10-10]
* @see [相关类/方法]
* @since [产品/模块版本]
*/
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();
}
}
/**
* **************************更快捷的打zip包的方法**************************************************************
*/
* 另一种方式打zip包------22 <功能详细描述>
*
* @param epubZipDir--源路径
* @param afterDrmPath ---目标路径
* @return [参数说明]
*
* @return long [返回类型说明]
* @exception throws [违例类型] [违例说明]
* @see [类、类#方法、类#成员]
*/
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");
}
}