⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 zipmanager.java

📁 java实现msn的功能
💻 JAVA
字号:
/*
 * @(#)ZipManager.java
 *
 * Copyright (c) 2001, JangHo Hwang
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 	1. Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *
 * 	2. Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 *
 * 	3. Neither the name of the JangHo Hwang nor the names of its contributors
 * may be used to endorse or promote products derived from this software
 * without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *    $Id: ZipManager.java,v 1.1.1.1 2002/03/08 21:20:46 xrath Exp $
 */
package rath.util;

import java.io.*;
import java.util.*;
import java.util.zip.*;
/**
 * Zip屈侥狼 颇老阑 拘绵阑 钱绊, 肚茄 颇老甸阑 隔酒辑
 * Zip栏肺 弓绢林绰 巴阑 埃祈窍霸 荤侩且 荐 乐绰
 * 牢磐其捞胶甫 力傍窍绰 努贰胶捞促.
 *
 * @author JangHo Hwang, rath@linuxkorea.co.kr
 * @version $Id: ZipManager.java,v 1.1.1.1 2002/03/08 21:20:46 xrath Exp $
 */
public class ZipManager
{
	// CRC update甫 且锭 荤侩且 buffer狼 农扁
	public static final int BUFFER_SIZE = 1024;
	// 颇老 版肺 备盒磊捞哥, 祈府茄 荤侩阑 困秦 static final肺 急攫
	public static final String FS = System.getProperty("file.separator");
	private String archiveRoot = System.getProperty("user.dir");

	/**
	 * 阿 弓阑 颇老狼 风飘 叼泛配府 困摹甫 沥茄促.
	 * 颇老甸捞 弓老锭绰, 阿 颇老狼 例措 版肺俊辑
	 * 捞 酒墨捞宏 风飘 叼泛配府福 力寇茄 版肺疙捞
	 * 角力 zip郴侩俊辑狼 Entry name捞 等促.
	 *
	 * @param    directory    酒墨捞宏 风飘 叼泛配府.
	 */
	public void setArchiveRoot( String directory )
	{
		this.archiveRoot = directory;
	}

	/**
	 * 泅犁 酒墨捞宏 风飘 叼泛配府甫 馆券茄促.
	 * @return    酒墨捞宏 风飘 叼泛配府.
	 */
	public String getArchiveRoot()
	{
		return archiveRoot;
	}

	public void doCompress( File file, File toCreate )
		throws IOException
	{
		if( toCreate.exists() )
			throw new IOException( toCreate + " is already exist" );

		FileOutputStream fos = new FileOutputStream( toCreate );
		doCompress( file, fos );
	}

	public void doCompress( File file, OutputStream out )
		throws IOException
	{
		ZipOutputStream zos = new ZipOutputStream( out );

		compress( file, zos );

		zos.setMethod( ZipOutputStream.DEFLATED );
		zos.close();
	}

	private void compress( File file, ZipOutputStream zos )
		throws IOException
	{
		if( file.isDirectory() )
		{
			File[] files = file.listFiles();
			for(int i=0; i<files.length; i++)
			{
				if( files[i].isDirectory() )
					compress( files[i], zos );
				else
					addFile( files[i], zos );
			}
		}
		else
		if( file.isFile() )
		{
			addFile( file, zos );
		}
	}

	public void addFile( File file, ZipOutputStream zos )
		throws IOException
	{
		if( Thread.currentThread().isInterrupted() )
			return;

		compressStarted( file );

		String enname = file.getAbsolutePath().substring(archiveRoot.length()+1);
		ZipEntry en = new ZipEntry( enname );
		CRC32 crc32 = new CRC32();

		byte[] chs = new byte[1024];

		FileInputStream fis = new FileInputStream(file);
		int len = 0;
		while( (len=fis.read( chs )) > -1 )
			crc32.update( chs, 0, len );
		fis.close();

		en.setSize( file.length() );
		en.setTime( file.lastModified() );
		en.setCrc( crc32.getValue() );

		zos.putNextEntry( en );
		fis = new FileInputStream(file);
		while( (len=fis.read( chs )) > -1 )
			zos.write( chs, 0, len );
		fis.close();
		zos.closeEntry();

		compressComplete( file );
	}

	/**
	 * 秦寸 颇老阑 拘绵窍扁 流傈俊 阂府款促.
	 *
	 * @param file
	 */
	protected void compressStarted( File file )
	{

	}

	/**
	 * 秦寸 颇老阑 拘绵阑 场辰 流 饶 阂府款促.
	 *
	 * @param file
	 */
	protected void compressComplete( File file )
	{

	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -