`

Android zip文件压缩解压

阅读更多

Android zip文件压缩解压

 

Android项目中需要将一些信息进行收集再进行压缩,最后将压缩文件上传到服务器中,以下代码实现此功能,并支持中文文件名

package com.example.androidzip.tools;

import java.io.File;
import java.util.ArrayList;
import java.util.LinkedList;

/**
 * 文件夹遍历
 * @author miaowei
 *
 */
public class DirTraversal {

	//no recursion
    public static LinkedList<File> listLinkedFiles(String strPath) {
        LinkedList<File> list = new LinkedList<File>();
        File dir = new File(strPath);
        File file[] = dir.listFiles();
        for (int i = 0; i < file.length; i++) {
            /*if (file[i].isDirectory()){
            	
            	list.add(file[i]);
            }else{
            	
            	System.out.println(file[i].getAbsolutePath());
            	
            }*/
        	list.add(file[i]); 
        }
        /*File tmp;
        while (!list.isEmpty()) {
            tmp = (File) list.removeFirst();
            if (tmp.isDirectory()) {
                file = tmp.listFiles();
                if (file == null)
                    continue;
                for (int i = 0; i < file.length; i++) {
                    if (file[i].isDirectory())
                        list.add(file[i]);
                    else
                        System.out.println(file[i].getAbsolutePath());
                }
            } else {
                System.out.println(tmp.getAbsolutePath());
            }
        }*/
        return list;
    }
 
     
    //recursion
    public static ArrayList<File> listFiles(String strPath) {
        return refreshFileList(strPath);
    }
 
    public static ArrayList<File> refreshFileList(String strPath) {
        ArrayList<File> filelist = new ArrayList<File>();
        File dir = new File(strPath);
        File[] files = dir.listFiles();
 
        if (files == null)
            return null;
        for (int i = 0; i < files.length; i++) {
            if (files[i].isDirectory()) {
                refreshFileList(files[i].getAbsolutePath());
            } else {
                if(files[i].getName().toLowerCase().endsWith("zip")){
                	
                	filelist.add(files[i]);
                }
                    
            }
        }
        return filelist;
    }
    
    public static ArrayList<File> arrayListFiles(String strPath){
    	
    	 ArrayList<File> filelist = new ArrayList<File>();
         File dir = new File(strPath);
         File[] files = dir.listFiles();
         for (int i = 0; i < files.length; i++) {
			
        	 filelist.add(files[i].getAbsoluteFile());
		}
         return filelist;
    }
    //-----4.0读取文件的报 open failed: ENOENT (No such file or directory)
    /**
	 * 1\可先创建文件的路径
	 * @param filePath
	 */
	public static void makeRootDirectory(String filePath) {
		File file = null;
		try {
			file = new File(filePath);
			if (!file.exists()) {
				file.mkdir();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
    /**
     * 2\然后在创建文件名就不会在报该错误
     * @param filePath
     * @param fileName
     * @return
     */
	public static File getFilePath(String filePath, String fileName) {
		File file = null;
		makeRootDirectory(filePath);
		try {
			file = new File(filePath + fileName);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return file;
	}

	
}

 

package com.example.androidzip.tools;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
/**
 * Java utils 实现的Zip工具
 * @author miaowei
 *
 */
public class ZipUtils {

	private static final int BUFF_SIZE = 1024 * 1024; // 1M Byte
	 
    /**
     * 批量压缩文件(夹)
     *
     * @param resFileList 要压缩的文件(夹)列表
     * @param zipFile 生成的压缩文件
     * @throws IOException 当压缩过程出错时抛出
     */
    public static void zipFiles(Collection<File> resFileList, File zipFile) throws IOException {
        ZipOutputStream zipout = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile), BUFF_SIZE));
        for (File resFile : resFileList) {
            zipFile(resFile, zipout, "");
        }
        zipout.close();
    }
 
    /**
     * 批量压缩文件(夹)
     *
     * @param resFileList 要压缩的文件(夹)列表
     * @param zipFile 生成的压缩文件
     * @param comment 压缩文件的注释
     * @throws IOException 当压缩过程出错时抛出
     */
    public static void zipFiles(Collection<File> resFileList, File zipFile, String comment)
            throws IOException {
        ZipOutputStream zipout = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(
                zipFile), BUFF_SIZE));
        for (File resFile : resFileList) {
            zipFile(resFile, zipout, "");
        }
        zipout.setComment(comment);
        zipout.close();
    }
 
    /**
     * 解压缩一个文件
     *
     * @param zipFile 压缩文件
     * @param folderPath 解压缩的目标目录
     * @throws IOException 当解压缩过程出错时抛出
     */
    public static void upZipFile(File zipFile, String folderPath) throws ZipException, IOException {
        File desDir = new File(folderPath);
        if (!desDir.exists()) {
            desDir.mkdirs();
        }
        ZipFile zf = new ZipFile(zipFile);
        for (Enumeration<?> entries = zf.entries(); entries.hasMoreElements();) {
            ZipEntry entry = ((ZipEntry)entries.nextElement());
            if (entry.isDirectory()) {
				
            	continue;
			}
            InputStream in = zf.getInputStream(entry);
            String str = folderPath + File.separator + entry.getName();
            str = new String(str.getBytes(), "utf-8");
            File desFile = new File(str);
            if (!desFile.exists()) {
                File fileParentDir = desFile.getParentFile();
                if (!fileParentDir.exists()) {
                    fileParentDir.mkdirs();
                }
                desFile.createNewFile();
            }
            OutputStream out = new FileOutputStream(desFile);
            byte buffer[] = new byte[BUFF_SIZE];
            int realLength;
            while ((realLength = in.read(buffer)) > 0) {
                out.write(buffer, 0, realLength);
            }
            in.close();
            out.close();
        }
    }
 
    /**
     * 解压文件名包含传入文字的文件
     *
     * @param zipFile 压缩文件
     * @param folderPath 目标文件夹
     * @param nameContains 传入的文件匹配名
     * @throws ZipException 压缩格式有误时抛出
     * @throws IOException IO错误时抛出
     */
    public static ArrayList<File> upZipSelectedFile(File zipFile, String folderPath,
            String nameContains) throws ZipException, IOException {
        ArrayList<File> fileList = new ArrayList<File>();
 
        File desDir = new File(folderPath);
        if (!desDir.exists()) {
            desDir.mkdir();
        }
 
        ZipFile zf = new ZipFile(zipFile);
        for (Enumeration<?> entries = zf.entries(); entries.hasMoreElements();) {
            ZipEntry entry = ((ZipEntry)entries.nextElement());
            if (entry.getName().contains(nameContains)) {
                InputStream in = zf.getInputStream(entry);
                String str = folderPath + File.separator + entry.getName();
                str = new String(str.getBytes("utf-8"), "gbk");
                // str.getBytes("GB2312"),"8859_1" 输出
                // str.getBytes("8859_1"),"GB2312" 输入
                File desFile = new File(str);
                if (!desFile.exists()) {
                    File fileParentDir = desFile.getParentFile();
                    if (!fileParentDir.exists()) {
                        fileParentDir.mkdirs();
                    }
                    desFile.createNewFile();
                }
                OutputStream out = new FileOutputStream(desFile);
                byte buffer[] = new byte[BUFF_SIZE];
                int realLength;
                while ((realLength = in.read(buffer)) > 0) {
                    out.write(buffer, 0, realLength);
                }
                in.close();
                out.close();
                fileList.add(desFile);
            }
        }
        return fileList;
    }
 
    /**
     * 获得压缩文件内文件列表
     *
     * @param zipFile 压缩文件
     * @return 压缩文件内文件名称
     * @throws ZipException 压缩文件格式有误时抛出
     * @throws IOException 当解压缩过程出错时抛出
     */
    public static ArrayList<String> getEntriesNames(File zipFile) throws ZipException, IOException {
        ArrayList<String> entryNames = new ArrayList<String>();
        Enumeration<?> entries = getEntriesEnumeration(zipFile);
        while (entries.hasMoreElements()) {
            ZipEntry entry = ((ZipEntry)entries.nextElement());
            entryNames.add(new String(getEntryName(entry).getBytes("GB2312"), "8859_1"));
        }
        return entryNames;
    }
 
    /**
     * 获得压缩文件内压缩文件对象以取得其属性
     *
     * @param zipFile 压缩文件
     * @return 返回一个压缩文件列表
     * @throws ZipException 压缩文件格式有误时抛出
     * @throws IOException IO操作有误时抛出
     */
    public static Enumeration<?> getEntriesEnumeration(File zipFile) throws ZipException,
            IOException {
        ZipFile zf = new ZipFile(zipFile);
        return zf.entries();
 
    }
 
    /**
     * 取得压缩文件对象的注释
     *
     * @param entry 压缩文件对象
     * @return 压缩文件对象的注释
     * @throws UnsupportedEncodingException
     */
    public static String getEntryComment(ZipEntry entry) throws UnsupportedEncodingException {
        return new String(entry.getComment().getBytes("GB2312"), "8859_1");
    }
 
    /**
     * 取得压缩文件对象的名称
     *
     * @param entry 压缩文件对象
     * @return 压缩文件对象的名称
     * @throws UnsupportedEncodingException
     */
    public static String getEntryName(ZipEntry entry) throws UnsupportedEncodingException {
        return new String(entry.getName().getBytes("GB2312"), "8859_1");
    }
 
    /**
     * 压缩文件
     *
     * @param resFile 需要压缩的文件(夹)
     * @param zipout 压缩的目的文件
     * @param rootpath 压缩的文件路径
     * @throws FileNotFoundException 找不到文件时抛出
     * @throws IOException 当压缩过程出错时抛出
     */
    private static void zipFile(File resFile, ZipOutputStream zipout, String rootpath)
            throws FileNotFoundException, IOException {
        rootpath = rootpath + (rootpath.trim().length() == 0 ? "" : File.separator)
                + resFile.getName();
        rootpath = new String(rootpath.getBytes(), "utf-8");
        if (resFile.isDirectory()) {
            File[] fileList = resFile.listFiles();
            for (File file : fileList) {
                zipFile(file, zipout, rootpath);
            }
        } else {
            byte buffer[] = new byte[BUFF_SIZE];
            BufferedInputStream in = new BufferedInputStream(new FileInputStream(resFile),
                    BUFF_SIZE);
            zipout.putNextEntry(new ZipEntry(rootpath));
            int realLength;
            while ((realLength = in.read(buffer)) != -1) {
                zipout.write(buffer, 0, realLength);
            }
            in.close();
            zipout.flush();
            zipout.closeEntry();
        }
    }
    
    //第二种实现
	public static void zip(String src, String dest) throws IOException {
		// 提供了一个数据项压缩成一个ZIP归档输出流
		ZipOutputStream out = null;
		try {

			//DirTraversal.makeRootDirectory(dest);
			//File outFile = DirTraversal.getFilePath(dest,"cache.zip");
			
			File outFile = new File(dest);// 源文件或者目录
			File fileOrDirectory = new File(src);// 压缩文件路径
			out = new ZipOutputStream(new FileOutputStream(outFile));
			// 如果此文件是一个文件,否则为false。
			if (fileOrDirectory.isFile()) {
				zipFileOrDirectory(out, fileOrDirectory, "");
			} else {
				// 返回一个文件或空阵列。
				File[] entries = fileOrDirectory.listFiles();
				for (int i = 0; i < entries.length; i++) {
					// 递归压缩,更新curPaths
					zipFileOrDirectory(out, entries[i], "");
				}
			}
		} catch (IOException ex) {
			ex.printStackTrace();
		} finally {
			// 关闭输出流
			if (out != null) {
				try {
					out.close();
				} catch (IOException ex) {
					ex.printStackTrace();
				}
			}
		}
	}

	private static void zipFileOrDirectory(ZipOutputStream out,
			File fileOrDirectory, String curPath) throws IOException {
		// 从文件中读取字节的输入流
		FileInputStream in = null;
		try {
			// 如果此文件是一个目录,否则返回false。
			if (!fileOrDirectory.isDirectory()) {
				// 压缩文件
				byte[] buffer = new byte[4096];
				int bytes_read;
				in = new FileInputStream(fileOrDirectory);
				// 实例代表一个条目内的ZIP归档
				ZipEntry entry = new ZipEntry(curPath
						+ fileOrDirectory.getName());
				// 条目的信息写入底层流
				out.putNextEntry(entry);
				while ((bytes_read = in.read(buffer)) != -1) {
					out.write(buffer, 0, bytes_read);
				}
				out.closeEntry();
			} else {
				// 压缩目录
				File[] entries = fileOrDirectory.listFiles();
				for (int i = 0; i < entries.length; i++) {
					// 递归压缩,更新curPaths
					zipFileOrDirectory(out, entries[i], curPath
							+ fileOrDirectory.getName() + "/");
				}
			}
		} catch (IOException ex) {
			ex.printStackTrace();
			// throw ex;
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException ex) {
					ex.printStackTrace();
				}
			}
		}
	}

	@SuppressWarnings("unchecked")
	public static void unzip(String zipFileName, String outputDirectory)
			throws IOException {
		ZipFile zipFile = null;
		try {
			zipFile = new ZipFile(zipFileName);
			Enumeration e = zipFile.entries();
			ZipEntry zipEntry = null;
			File dest = new File(outputDirectory);
			dest.mkdirs();
			while (e.hasMoreElements()) {
				zipEntry = (ZipEntry) e.nextElement();
				String entryName = zipEntry.getName();
				InputStream in = null;
				FileOutputStream out = null;
				try {
					if (zipEntry.isDirectory()) {
						String name = zipEntry.getName();
						name = name.substring(0, name.length() - 1);
						File f = new File(outputDirectory + File.separator
								+ name);
						f.mkdirs();
					} else {
						int index = entryName.lastIndexOf("\\");
						if (index != -1) {
							File df = new File(outputDirectory + File.separator
									+ entryName.substring(0, index));
							df.mkdirs();
						}
						index = entryName.lastIndexOf("/");
						if (index != -1) {
							File df = new File(outputDirectory + File.separator
									+ entryName.substring(0, index));
							df.mkdirs();
						}
						File f = new File(outputDirectory + File.separator
								+ zipEntry.getName());
						// f.createNewFile();
						in = zipFile.getInputStream(zipEntry);
						out = new FileOutputStream(f);
						int c;
						byte[] by = new byte[1024];
						while ((c = in.read(by)) != -1) {
							out.write(by, 0, c);
						}
						out.flush();
					}
				} catch (IOException ex) {
					ex.printStackTrace();
					throw new IOException("解压失败:" + ex.toString());
				} finally {
					if (in != null) {
						try {
							in.close();
						} catch (IOException ex) {
						}
					}
					if (out != null) {
						try {
							out.close();
						} catch (IOException ex) {
						}
					}
				}
			}
		} catch (IOException ex) {
			ex.printStackTrace();
			throw new IOException("解压失败:" + ex.toString());
		} finally {
			if (zipFile != null) {
				try {
					zipFile.close();
				} catch (IOException ex) {
				}
			}
		}
	}
}

 

package com.example.androidzip;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.LinkedList;
import java.util.zip.ZipException;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.example.androidzip.tools.DirTraversal;
import com.example.androidzip.tools.ZipUtils;
/**
 * Android zip文件压缩解压缩
 * @author miaowei
 *
 */
public class MainActivity extends Activity {

	/**
	 * 压缩
	 */
	private Button btn_zip;
	/**
	 * 解压
	 */
	private Button btn_unzip;
	
	String pathString = Environment.getExternalStorageDirectory().getAbsolutePath();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		btn_unzip = (Button)findViewById(R.id.btn_unzip);
		
		btn_zip = (Button)findViewById(R.id.btn_zip);
		btn_zip.setOnClickListener(onClickListener);
		btn_unzip.setOnClickListener(onClickListener);
	}
	
	
	private OnClickListener onClickListener = new OnClickListener(){
		
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.btn_zip:

				try {
					//测试数据,注意更换目录
					LinkedList<File> files = DirTraversal.listLinkedFiles(pathString+"/Android/data/com.mapbar.info.collection/files/cache");
					
					File file = DirTraversal.getFilePath(pathString+"/Android/data/com.mapbar.info.collection/files/", "cache.zip");
					
					ZipUtils.zipFiles(files, file);
					
					//第二种实现
					//ZipUtils.zip(pathString+"/Android/data/com.mapbar.info.collection/files/cache", pathString+"/Android/data/com.mapbar.info.collection/files/cache.zip");
					//ZipUtils.unzip(pathString+"/Android/data/com.mapbar.info.collection/files/cache.zip", pathString+"/Android/data/com.mapbar.info.collection/files/cache");
				} catch (IOException e) {
					e.printStackTrace();
				}
				break;
			case R.id.btn_unzip:
				File file = DirTraversal.getFilePath(pathString+"/Android/data/com.mapbar.info.collection/files/", "cache.zip");
				try {
					ZipUtils.upZipFile(file, pathString+"/Android/data/com.mapbar.info.collection/files/cachezip");
				} catch (ZipException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
				break;
			default:
				break;
			}
		
		
		}
		
	};
	
}

 以下为处理乱码转换字符串的编码

/** 
* 转换字符串的编码 
*/  
public class ChangeCharset {  
/** 7位ASCII字符,也叫作ISO646-US、Unicode字符集的基本拉丁块 */  
public static final String US_ASCII = "US-ASCII";  
  
/** ISO 拉丁字母表 No.1,也叫作 ISO-LATIN-1 */  
public static final String ISO_8859_1 = "ISO-8859-1";  
  
/** 8 位 UCS 转换格式 */  
public static final String UTF_8 = "UTF-8";  
  
/** 16 位 UCS 转换格式,Big Endian(最低地址存放高位字节)字节顺序 */  
public static final String UTF_16BE = "UTF-16BE";  
  
/** 16 位 UCS 转换格式,Little-endian(最高地址存放低位字节)字节顺序 */  
public static final String UTF_16LE = "UTF-16LE";  
  
/** 16 位 UCS 转换格式,字节顺序由可选的字节顺序标记来标识 */  
public static final String UTF_16 = "UTF-16";  
  
/** 中文超大字符集 */  
public static final String GBK = "GBK";  
  
/** 
* 将字符编码转换成US-ASCII码 
*/  
public String toASCII(String str) throws UnsupportedEncodingException {  
return this.changeCharset(str, US_ASCII);  
}  
/** 
* 将字符编码转换成ISO-8859-1码 
*/  
public String toISO_8859_1(String str) throws UnsupportedEncodingException {  
return this.changeCharset(str, ISO_8859_1);  
}  
/** 
* 将字符编码转换成UTF-8码 
*/  
public String toUTF_8(String str) throws UnsupportedEncodingException {  
return this.changeCharset(str, UTF_8);  
}  
/** 
* 将字符编码转换成UTF-16BE码 
*/  
public String toUTF_16BE(String str) throws UnsupportedEncodingException {  
return this.changeCharset(str, UTF_16BE);  
}  
/** 
* 将字符编码转换成UTF-16LE码 
*/  
public String toUTF_16LE(String str) throws UnsupportedEncodingException {  
return this.changeCharset(str, UTF_16LE);  
}  
/** 
* 将字符编码转换成UTF-16码 
*/  
public String toUTF_16(String str) throws UnsupportedEncodingException {  
return this.changeCharset(str, UTF_16);  
}  
/** 
* 将字符编码转换成GBK码 
*/  
public String toGBK(String str) throws UnsupportedEncodingException {  
return this.changeCharset(str, GBK);  
}  
  
/** 
* 字符串编码转换的实现方法 
* @param str 待转换编码的字符串 
* @param newCharset 目标编码 
* @return 
* @throws UnsupportedEncodingException 
*/  
public String changeCharset(String str, String newCharset)  
throws UnsupportedEncodingException {  
if (str != null) {  
//用默认字符编码解码字符串。  
byte[] bs = str.getBytes();  
//用新的字符编码生成字符串  
return new String(bs, newCharset);  
}  
return null;  
}  
/** 
* 字符串编码转换的实现方法 
* @param str 待转换编码的字符串 
* @param oldCharset 原编码 
* @param newCharset 目标编码 
* @return 
* @throws UnsupportedEncodingException 
*/  
public String changeCharset(String str, String oldCharset, String newCharset)  
throws UnsupportedEncodingException {  
if (str != null) {  
//用旧的字符编码解码字符串。解码可能会出现异常。  
byte[] bs = str.getBytes(oldCharset);  
//用新的字符编码生成字符串  
return new String(bs, newCharset);  
}  
return null;  
}  
  
public static void main(String[] args) throws UnsupportedEncodingException {  
ChangeCharset test = new ChangeCharset();  
String str = "This is a 中文的 String!";  
System.out.println("str: " + str);  
String gbk = test.toGBK(str);  
System.out.println("转换成GBK码: " + gbk);  
System.out.println();  
String ascii = test.toASCII(str);  
System.out.println("转换成US-ASCII码: " + ascii);  
gbk = test.changeCharset(ascii,ChangeCharset.US_ASCII, ChangeCharset.GBK);  
System.out.println("再把ASCII码的字符串转换成GBK码: " + gbk);  
System.out.println();  
String iso88591 = test.toISO_8859_1(str);  
System.out.println("转换成ISO-8859-1码: " + iso88591);  
gbk = test.changeCharset(iso88591,ChangeCharset.ISO_8859_1, ChangeCharset.GBK);  
System.out.println("再把ISO-8859-1码的字符串转换成GBK码: " + gbk);  
System.out.println();  
String utf8 = test.toUTF_8(str);  
System.out.println("转换成UTF-8码: " + utf8);  
gbk = test.changeCharset(utf8,ChangeCharset.UTF_8, ChangeCharset.GBK);  
System.out.println("再把UTF-8码的字符串转换成GBK码: " + gbk);  
System.out.println();  
String utf16be = test.toUTF_16BE(str);  
System.out.println("转换成UTF-16BE码:" + utf16be);  
gbk = test.changeCharset(utf16be,ChangeCharset.UTF_16BE, ChangeCharset.GBK);  
System.out.println("再把UTF-16BE码的字符串转换成GBK码: " + gbk);  
System.out.println();  
String utf16le = test.toUTF_16LE(str);  
System.out.println("转换成UTF-16LE码:" + utf16le);  
gbk = test.changeCharset(utf16le,ChangeCharset.UTF_16LE, ChangeCharset.GBK);  
System.out.println("再把UTF-16LE码的字符串转换成GBK码: " + gbk);  
System.out.println();  
String utf16 = test.toUTF_16(str);  
System.out.println("转换成UTF-16码:" + utf16);  
gbk = test.changeCharset(utf16,ChangeCharset.UTF_16LE, ChangeCharset.GBK);  
System.out.println("再把UTF-16码的字符串转换成GBK码: " + gbk);  
String s = new String("中文".getBytes("UTF-8"),"UTF-8");  
System.out.println(s);  
}  
}  

附件中有源码供参考

分享到:
评论

相关推荐

    Android将assets中的zip压缩文件解压到SD卡

    在Android开发中,有时我们需要将应用内部的资源文件,如ZIP压缩文件,解压到外部存储(即SD卡)上,以便用户可以访问或使用这些数据。本文将详细讲解如何实现这一功能,主要涉及Android权限管理、文件操作以及ZIP...

    android ZIP文件的解压

    在Android平台上,ZIP文件的解压是一个常见的任务,特别是在应用程序安装、数据包下载以及资源管理等方面。ZIP是一种广泛使用的文件格式,它允许将多个文件和目录打包成一个单一的文件,便于存储和传输。以下是对ZIP...

    Android端zip压缩与解压.zip

    Android端zip压缩与解压,目前暂时只做zip格式支持,基于Zip4j (http://www.lingala.net/zip4j/)进行扩展成工具类,支持对单个文件,多个文件以及文件夹进行压缩,对压缩文件解压到到指定目录,支持压缩解压使用密码...

    java android zip解压缩(解决压缩中文乱码问题)

    本篇文章将深入探讨如何在Android平台上解决Java ZIP库在解压缩中文文件时出现的乱码问题。 首先,我们要明白乱码问题的根源。在文件的压缩和解压缩过程中,文件名通常被编码为字节序列,这个序列取决于原始文件名...

    Android实现下载zip压缩文件并解压的方法(附源码)

    其实在网上有很多介绍下载文件或者解压zip文件的文章,但是两者结合的不多,所以这篇文章在此记录一下下载zip文件并直接解压的方法,直接上代码,文末有源码下载。 下载: import java.io.BufferedInputStream; ...

    android Zip解压、压缩

    本文将深入探讨如何在Android平台上使用Zip库进行文件的压缩和解压缩操作,以满足日常开发需求。 首先,让我们了解什么是Zip格式。Zip是一种广泛使用的文件归档格式,它允许将多个文件和目录打包成一个单一的压缩...

    Android-Android端zip压缩与解压支持使用密码对单文件多文件文件夹进行压缩以及解压操作

    总结,通过`Zip4j`库,Android开发者可以轻松实现ZIP文件的压缩和解压缩,同时支持密码保护,提高了应用的功能性和安全性。在实际项目中,应根据需求选择合适的压缩算法和优化策略,以达到最佳的性能和用户体验。

    Android ZIP文件下载以及解压

    在Android平台上,开发人员经常需要处理ZIP文件,例如下载、解压和操作文件。这篇文章将深入探讨如何在Android应用中实现ZIP文件的下载与解压功能。首先,我们需要理解ZIP文件格式,它是一种广泛使用的文件归档格式...

    android 解压zip文件

    在Android平台上,处理zip文件是常见的任务,无论是为了分发应用程序、存储数据还是进行资源管理。本文将深入探讨如何在Android中解压zip文件,并结合打包zip文件的概念,以帮助你更好地理解整个过程。 首先,我们...

    安卓文件下载上传解压相关-AndroidZIP文件下载以及解压.zip

    综上所述,Android应用开发中处理ZIP文件下载和解压涉及多种技术,包括文件I/O、网络请求和文件系统操作。开发者需要熟悉这些基本概念,才能有效地实现功能。在实际项目中,还可能需要结合其他工具库如Volley、...

    Android的zip解压缩

    本文将深入探讨如何在Android平台上进行zip文件的解压缩操作。 首先,我们需要理解zip文件的基本概念。Zip是一种常用的文件格式,用于存储多个文件和目录在一个单一的、可压缩的档案中。在Android上,我们可以使用...

    Android 在线下载压缩包并解压到指定目录.zip

    避免在用户可控制的路径上解压文件。 10. **资源释放**: 完成下载和解压后,记得关闭所有打开的流,如`FileOutputStream`、`ZipInputStream`等,以防止内存泄漏。 综上所述,实现Android在线下载并解压压缩包到...

    Android对Zip文件的加压和解压

    3. **解压文件**:对于每个条目,我们需要确定它是文件还是目录。如果是文件,我们需要创建一个新的输出流,并将Zip条目的数据写入到这个输出流。如果是目录,我们需要在目标路径下创建相应的目录结构。 ```java if...

    android zip压缩demo

    在Android开发中,压缩和解压文件是常见的需求,例如保存用户数据、更新应用程序资源或者在后台传输数据。本示例“android zip压缩demo”提供了一个实际应用在项目中的例子,确保了其功能的可靠性和实用性。下面我们...

    Android Java zip解压库

    6. **安全性和权限管理**: 在Android中,由于存储权限的限制,解压文件时可能需要请求用户权限。此外,库可能也考虑了文件安全问题,避免了路径遍历攻击等潜在风险。 7. **异步操作**: 为了不阻塞UI线程,解压操作...

    android 解压.ZIP文件实例

    3. 解压文件:对于每个条目,如果它是一个文件,我们需要创建目标目录(如果不存在),然后创建一个`FileOutputStream`来写入解压后的文件数据。调用`ZipInputStream`的`read()`方法读取ZIP文件中的数据,并通过`...

    javaandroid可用的ziprar解压缩代码实现.rar

    在Android平台上,对ZIP和RAR文件进行解压缩是常见的需求,比如在安装APK应用、更新资源文件或者处理用户上传的数据时。JavaAndroid可用的ziprar解压缩代码实现提供了这样的功能,但请注意,由于文件数量多,可能...

    android 解压 zip4j

    在Android平台上进行文件操作时,有时我们需要对ZIP格式的压缩包进行解压,这时可以借助第三方库如`zip4j`。`zip4j`是一个强大的Java库,它提供了全面的功能来处理ZIP文件,包括创建、读取、更新、删除ZIP文件以及解...

    android rar,zip解压

    在Android平台上,处理rar和zip压缩文件是常见的需求,尤其对于开发者来说,了解如何进行解压操作至关重要。本文将深入探讨如何在Java和Android环境中实现rar及zip文件的解压功能,并提供相应的调用方法。 首先,...

Global site tag (gtag.js) - Google Analytics