脚本之家

电脑版
提示:原网页已由神马搜索转码, 内容由www.jb51.net提供.
您的位置:首页软件编程Android→ Android文件压缩解压

Android实现文件压缩与解压工具类

  更新时间:2024年04月24日 09:40:44  作者:generallizhong 
这篇文章主要为大家详细介绍了如何使用Android实现一个文件压缩与解压工具类,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下

一个简单压缩解压工具类

public class ZipUtils {
public static void compressFolder(Activity activity, String sourcePath, String zipFile) throws IOException {
File folder = new File(sourcePath);
File zipPath = new File(zipFile);
if (zipPath.exists()) {
zipPath.delete();
}

// 创建输出流并指定要生成的ZIP文件路径
try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile))) {
addFilesToZip(folder, folder.getName(), zos);

System.out.println("文件夹已成功压缩为 " + zipFile);
Toast.makeText(activity, "压缩已完成", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
throw new RuntimeException("无法将文件夹压缩到ZIP文件中", e);
}
}

private static void addFilesToZip(File file, String parentName, ZipOutputStream zos) throws IOException {
if (!file.exists()) return;

for (File child : file.listFiles()) {
if (child.isDirectory()) {
addFilesToZip(child, parentName + "/" + child.getName(), zos);
} else {
byte[] buffer = new byte[1024];

// 获取当前文件相对于源文件夹的路径名称
String entryName = parentName + "/" + child.getName();

// 添加新条目到ZIP文件中
zos.putNextEntry(new ZipEntry(entryName));

// 读取文件内容并写入ZIP文件
try (InputStream is = new FileInputStream(child)) {
int length;
while ((length = is.read(buffer)) > 0) {
zos.write(buffer, 0, length);
}
} finally {
zos.closeEntry();
}
}
}
}

public static void Unzip(String zipFile, String targetDir) {
Log.e("lz>>","zipFile:"+zipFile+" targetDir:"+targetDir);
int BUFFER = 4096; //这里缓冲区我们使用4KB,
String strEntry; //保存每个zip的条目名称

try {
BufferedOutputStream dest = null; //缓冲输出流
FileInputStream fis = new FileInputStream(zipFile);
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
ZipEntry entry; //每个zip条目的实例

while ((entry = zis.getNextEntry()) != null) {

try {
Log.i("Unzip: ","="+ entry);
int count;
byte data[] = new byte[BUFFER];
strEntry = entry.getName();

File entryFile = new File(targetDir + strEntry);
File entryDir = new File(entryFile.getParent());
if (!entryDir.exists()) {
entryDir.mkdirs();
}

FileOutputStream fos = new FileOutputStream(entryFile);
dest = new BufferedOutputStream(fos, BUFFER);
while ((count = zis.read(data, 0, BUFFER)) != -1) {
dest.write(data, 0, count);
}
dest.flush();
dest.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
zis.close();
Log.i("Unzip: ","="+ "结束");
} catch (Exception cwj) {
cwj.printStackTrace();
}
}

压缩使用:

               File appDir = getFilesDir();
String filePath = appDir+“”";//需要压缩的文件路径
String zipWalletfile = zipfile.getAbsolutePath();
try {
//zipWalletfile + "/wallet.zip"压缩后的文件名
ZipUtils.compressFolder(ZipActivity.this, filePath , zipWalletfile + "/wallet.zip");
} catch (Exception e) {
throw new RuntimeException(e);
}

解压使用:

   File zipfile = Environment.getExternalStorageDirectory();
String zipWalletfile = zipfile.getAbsolutePath();//待解压文件
String unzipPath=getFilesDir().getPath();解压文件路径
ZipUtils.Unzip(zipWalletfile + "/wallet.zip",unzipPath+"/");

到此这篇关于Android实现文件压缩与解压工具类的文章就介绍到这了,更多相关Android文件压缩解压内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 最新评论