WEB开发网
开发学院手机开发Android 开发 仿墨迹天气实现-->自定义扩展名的zip格式的皮肤... 阅读

仿墨迹天气实现-->自定义扩展名的zip格式的皮肤

 2012-09-06 13:56:01 来源:WEB开发网   
核心提示: 3. Re_Skin2Activity: package com.tony.skin;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.g
 
 
 
3. Re_Skin2Activity:
 
 
package com.tony.skin;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.tony.skin.utils.ZipUtil;

/**
 * 
 * @author Tony
 *
 */
public class Re_Skin2Activity extends Activity implements OnClickListener{
	private Button	btnSet;
	private Button	btnImport;
	private LinearLayout layout;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btnSet = (Button)findViewById(R.id.button1);
        btnSet.setOnClickListener(this);

        btnImport = (Button)findViewById(R.id.button2);
        btnImport.setOnClickListener(this);
        layout = (LinearLayout)findViewById(R.id.layout);
    }
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.button1:
			Bitmap bitmap= BitmapFactory.decodeFile("/sdcard/Skin_kris/skin/google.png");
			
			 BitmapDrawable bd=new BitmapDrawable(bitmap);
			btnSet.setBackgroundDrawable(bd);
			
			layout.setBackgroundDrawable(new BitmapDrawable(BitmapFactory.decodeFile("/sdcard/Skin_kris/skin/bg/bg.png")));
			
			break;
		case R.id.button2:
			ZipUtil zipp = new ZipUtil(2049);
			System.out.println("begin do zip");
			zipp.unZip("/sdcard/skin.zip","/sdcard/Skin_kris");
			Toast.makeText(this, "导入成功", Toast.LENGTH_SHORT).show();
			break;
		default:
			break;
		}
	}
}
 
 
 
 
4. ZipUtil 解压缩处理ZIP包的工具类
 
 
 
package com.tony.skin.utils;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.Deflater;

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;

/**
 * Zip包压缩,解压处理工具类
 * @author a
 *
 */
public class ZipUtil {
	private ZipFile         zipFile; 
    private ZipOutputStream zipOut;     //压缩Zip 
    private  int      		bufSize;    //size of bytes 
    private byte[]          buf; 
    private int             readedBytes; 
    public ZipUtil(){ 
        this(512); 
    } 

    public ZipUtil(int bufSize){ 
        this.bufSize = bufSize; 
        this.buf = new byte[this.bufSize]; 
    } 
     
	/**
	 * 
	 * @param srcFile  需要 压缩的目录或者文件
	 * @param destFile 压缩文件的路径
	 */
	public void doZip(String srcFile, String destFile) {// zipDirectoryPath:需要压缩的文件夹名
		File zipDir;
		String dirName;

		zipDir = new File(srcFile);
		dirName = zipDir.getName();
		try {
			this.zipOut = new ZipOutputStream(new BufferedOutputStream(
					new FileOutputStream(destFile)));
			//设置压缩的注释
			zipOut.setComment("comment");
			//设置压缩的编码,如果要压缩的路径中有中文,就用下面的编码
			zipOut.setEncoding("GBK");
			//启用压缩 
			zipOut.setMethod(ZipOutputStream.DEFLATED); 

			//压缩级别为最强压缩,但时间要花得多一点 
			zipOut.setLevel(Deflater.BEST_COMPRESSION); 
			
			handleDir(zipDir, this.zipOut,dirName);
			this.zipOut.close();
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}
	}

	/**
	 *  由doZip调用,递归完成目录文件读取
	 * @param dir
	 * @param zipOut
	 * @param dirName  这个主要是用来记录压缩文件的一个目录层次结构的
	 * @throws IOException
	 */
	private void handleDir(File dir, ZipOutputStream zipOut,String dirName) throws IOException {
		System.out.println("遍历目录:"+dir.getName());
		FileInputStream fileIn;
		File[] files;

		files = dir.listFiles();

		if (files.length == 0) {// 如果目录为空,则单独创建之.
			// ZipEntry的isDirectory()方法中,目录以"/"结尾.
			System.out.println("压缩的 Name:"+dirName);
			this.zipOut.putNextEntry(new ZipEntry(dirName));
			this.zipOut.closeEntry();
		} else {// 如果目录不为空,则分别处理目录和文件.
			for (File fileName : files) {
				// System.out.println(fileName);

				if (fileName.isDirectory()) {
					handleDir(fileName, this.zipOut,dirName+File.separator+fileName.getName()+File.separator);
				} else {
					System.out.println("压缩的 Name:"+dirName + File.separator+fileName.getName());
					fileIn = new FileInputStream(fileName);
					this.zipOut.putNextEntry(new ZipEntry(dirName + File.separator+fileName.getName()));

					while ((this.readedBytes = fileIn.read(this.buf)) > 0) {
						this.zipOut.write(this.buf, 0, this.readedBytes);
					}

					this.zipOut.closeEntry();
				}
			}
		}
	}

	/**
	 * 解压指定zip文件
	 * @param unZipfile 压缩文件的路径
	 * @param destFile   解压到的目录 
	 */
	public void unZip(String unZipfile, String destFile) {// unZipfileName需要解压的zip文件名
		FileOutputStream fileOut;
		File file;
		InputStream inputStream;

		try {
			this.zipFile = new ZipFile(unZipfile);

			for (Enumeration entries = this.zipFile.getEntries(); entries
					.hasMoreElements();) {
				ZipEntry entry = (ZipEntry) entries.nextElement();
				file = new File(destFile+File.separator+entry.getName());

				if (entry.isDirectory()) {
					file.mkdirs();
				} else {
					// 如果指定文件的目录不存在,则创建之.
					File parent = file.getParentFile();
					if (!parent.exists()) {
						parent.mkdirs();
					}

					inputStream = zipFile.getInputStream(entry);

					fileOut = new FileOutputStream(file);
					while ((this.readedBytes = inputStream.read(this.buf)) > 0) {
						fileOut.write(this.buf, 0, this.readedBytes);
					}
					fileOut.close();

					inputStream.close();
				}
			}
			this.zipFile.close();
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}
	}

	// 设置缓冲区大小
	public void setBufSize(int bufSize) {
		this.bufSize = bufSize;
	}
}
 
 
 
 

上一页  1 2 3 

Tags:墨迹 天气 实现

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接