增量打包工具

主要代码

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class Main
{

	private ConfigUtil util;
	private int count;
	private String zipName;

	public Main()
	{
		util = ConfigUtil.getInstance();
	}

	public static void main(String args[])
		throws Exception
	{
		Main t = new Main();
		t.doCopy();
		System.out.println();
		System.out.println((new StringBuilder("total file: ")).append(t.count).toString());
		System.out.println((new StringBuilder("created zip file: ")).append(t.zipName).toString());
	}

	private void doCopy()
		throws IOException
	{
//读取需要打包的文件
		File f = new File("file.properties");
		BufferedReader reader = new BufferedReader(new FileReader(f));
		zipName = (new StringBuilder(String.valueOf(util.getDescFolder()))).append(util.getZipFileName()).toString();
//准备Zip包 进行打包
		ZipOutputStream zipStream = new ZipOutputStream(new FileOutputStream(zipName));
		File temp = null;
		for (String line = null; (line = reader.readLine()) != null;)
		{
			line = line.trim();
			if (line.length() != 0)
			{
				if (line.startsWith(util.getSrcFolder()))
					line = (new StringBuilder(String.valueOf(util.getClassFolder()))).append(line.substring(util.getSrcFolder().length())).toString();
				if (line.endsWith(".java"))
				{
					line = line.replaceFirst("\\.java", "\\.class");
					temp = new File(line);
					copySinleFile(zipStream, temp);
					File files[] = temp.getParentFile().listFiles();
					File afile[];
					int j = (afile = files).length;
					for (int i = 0; i < j; i++)
					{
						File child = afile[i];
						if (isRelatedClassFile(child, temp))
							copySinleFile(zipStream, child);
					}

				} else
				{
					temp = new File(line);
					if (temp.isDirectory())
						copyChildFiles(zipStream, temp);
					else
						copySinleFile(zipStream, temp);
				}
			}
		}

		reader.close();
		zipStream.close();
	}

	private void copySinleFile(ZipOutputStream zipStream, File src)
		throws IOException
	{
		String desc = src.getAbsolutePath().substring(util.getRootFolder().length());
		zipStream.putNextEntry(new ZipEntry(desc));
		FileInputStream fis = new FileInputStream(src);
		byte buf[] = new byte[1024];
		for (int i = 0; (i = fis.read(buf)) != -1;)
			zipStream.write(buf, 0, i);

		zipStream.closeEntry();
		fis.close();
		count++;
		System.out.println((new StringBuilder("add File: ")).append(desc).toString());
	}

	private void copyChildFiles(ZipOutputStream zipStream, File folder)
		throws IOException
	{
		File files[] = folder.listFiles();
		File afile[];
		int j = (afile = files).length;
		for (int i = 0; i < j; i++)
		{
			File f = afile[i];
			if (f.isDirectory())
				copyChildFiles(zipStream, f);
			else
				copySinleFile(zipStream, f);
		}

	}

	private boolean isRelatedClassFile(File child, File temp)
	{
		String childName = child.getName();
		boolean value = false;
		if (childName.endsWith(".class"))
		{
			childName = childName.substring(0, childName.length() - 6);
			String tempName = temp.getName().substring(0, temp.getName().length() - 6);
			value = childName.startsWith((new StringBuilder(String.valueOf(tempName))).append("$").toString());
		}
		return value;
	}
}

你可能感兴趣的:(工具)