安卓解压zip文件,解压后多级目录显示

本文介绍了如何在安卓应用中利用ziputils库进行zip文件的解压,以节省资源,避免导入额外的jar包。解压过程中指定了目标路径,并在解压完成后直接跳转到解压的根目录,以便用户浏览和操作。通过示例代码展示了下载zip文件、解压文件及跳转到解压目录的操作流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

     因为最近我们的项目当中需要加入zip的解压,因为我们的项目本省就很大了,所以为了节省资源,我决定用ziputils,全部是代码不需要导入jar-包。

    

     我整体的思路是这样,首先解压,解压需要传入一个,解压文件路径,和解压完后才能之后的路径,解压完成之后需要直接跳到解压完成的根目录下,显示出来,并且可以继续点击,可查看文件可以查看,大体是这个效果。看下面的图组。

    

     这个是我们的项目中的所以会有这个云盘模块,从控制台发送文件到客户端,点击.zip文件下载到指定目录下面。

     下面代码是解压的逻辑,就一句话标红色的是。

   

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    initPermission();
    path = Environment.getExternalStorageDirectory().getAbsolutePath();
    zipPath = path + "/android.zip";
    destPath = path+ "/OutPath/";
    zipUtil = new ZipUtil();
    new Thread(){
        @Override
        public void run() {
            super.run();
            try {
                zipUtil.Ectract(zipPath,destPath);
            } catch (Exception e) {
               e.printStackTrace();
            }
        }
    }.start();
}

   下载完成之后直接跳到指定目录下。

                                                          

       这个是直接写了一个activity,然后跳转过去直接intent.pitStringExar(path)传入进去一个路径。

      

//打开文件
Intent zipIntent = new Intent(mActivity, ZipActivity.class);
zipIntent.putExtra("zipfile", newOutPath);//newPath是解压后的路径
startActivity(zipIntent);

           


    
    
    上面四张图片就是一级一级的点击打开,最后只打开的txt文档。并且左上角是一个返回键,只需要加上任务栈记住即可。以下是ZipActivity的代码
    
    
public class ZipActivity extends RootActivity {
    private ListView mListviewZip;
    private ImageView mIvback;
    private LocalFileAdapter mZipAdapter;
    public static String ZIPFILE = "zipfile";
    private List<File> mZipFileList;
    private String zipFilePath;
    private DocManager mDocManager;
    // path的堆栈
    private static Stack<String> mFilePathStack;
    private Handler mHandler = new Handler();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zip);
        initZipFile();
        initViews();
    }


    /**
     * 初始化zipfile的数据
     */
    private void initZipFile() {
        if (mZipFileList == null) {
            mZipFileList = new ArrayList<>();
        }
        zipFilePath = getIntent().getStringExtra(ZIPFILE);
        addPath(zipFilePath);
    }

private void initViews() {
    mDocManager = DocManager.getInstance(this);
    mIvback = (ImageView) findViewById(R.id.iv_back);
    mIvback.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (getLastPath().equals(zipFilePath)) {
                ToastManager.show(ZipActivity.this, R.string.file_up_root);
                return;
            }
            removeLastPath();//移除最上层任务栈
            searchViewData(getLastPath());//获取现在最底层的路径
        }
    });
    mListviewZip = (ListView) findViewById(R.id.list_zip);
    mZipFileList = FileUtil.getSortedFiles(zipFilePath);
    mZipAdapter = new LocalFileAdapter(this, mZipFileList);
    mListviewZip.setAdapter(mZipAdapter);
    mListviewZip.setOnItemClickListener(new ListView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
           String secondPath = mZipFileList.get(position).getAbsolutePath();
            File secondFile = new File(secondPath);
            if(secondFile.isFile()) {
                openTheFile(secondPath);
            }else if(secondFile.isDirectory()){
              searchViewData(secondPath);
                addPath(secondPath);
            }
        }
    });
}

    private void openTheFile(String secondPath) {
        final Intent intent = mDocManager.getIntentForFile(secondPath);
        GdLog.i("the file intent is= " + intent);
        if (intent != null) {
            mHandler.post(() -> {
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            });
        }else {
            mHandler.post(() -> {
                ToastManager.show(ZipActivity.this,"此文件类型不能被打开");
            });
        }
    }

    /**
     * 查询view的数据
     */
    public void searchViewData(final String path) {
        mZipFileList = FileUtil.getSortedFiles(path);
        if (mZipFileList.size() > 0) {
            mZipAdapter.setFiles(mZipFileList);
            mZipAdapter.setSelectedPosition(-1);
            mZipAdapter.notifyDataSetChanged();
        }
    }

 /**
     * 添加路径到堆栈
     *
     * @param path
     */
    public void addPath(String path) {
        if (mFilePathStack == null) {
            mFilePathStack = new Stack<String>();
        }
        mFilePathStack.add(path);
    }

    /**
     * 获取堆栈最上层的路径
     *
     * @return
     */
    public String getLastPath() {
        return mFilePathStack.lastElement();
    }

    /**
     * 移除堆栈最上层路径
     */
    public void removeLastPath() {
        mFilePathStack.remove(getLastPath());
    }
}


    下面是ZipUtils的代码:
   
public class ZipUtil {
/**
 * 解压缩
 *
 * @param sZipPathFile 要解压的文件
 * @param sDestPath    解压到某文件夹
 * @return
 */
@SuppressWarnings("unchecked")
public static ArrayList Ectract(String sZipPathFile, String sDestPath) {
    ArrayList<String> allFileName = new ArrayList<String>();
    try {
        // 先指定压缩档的位置和档名,建立FileInputStream对象
        FileInputStream fins = new FileInputStream(sZipPathFile);
        // 将fins传入ZipInputStream中
        ZipInputStream zins = new ZipInputStream(fins);
        ZipEntry ze = null;
        byte[] ch = new byte[256];
        while ((ze = zins.getNextEntry()) != null) {
            File zfile = new File(sDestPath + ze.getName());
            File fpath = new File(zfile.getParentFile().getPath());
            if (ze.isDirectory()) {
                if (!zfile.exists())
                    zfile.mkdirs();
                zins.closeEntry();
            } else {
                if (!fpath.exists())
                    fpath.mkdirs();
                FileOutputStream fouts = new FileOutputStream(zfile);
                int i;
                allFileName.add(zfile.getAbsolutePath());
                while ((i = zins.read(ch)) != -1)
                    fouts.write(ch, 0, i);
                zins.closeEntry();
                fouts.close();
            }
        }
        fins.close();
        zins.close();
    } catch (Exception e) {
        System.err.println("Extract error:" + e.getMessage());
    }
    return allFileName;
}
}
最后是一个adapter和xml文件的代码了,那个我就不传了,因为没有单独写demo,直接写到了公司的项目中,再传的话看起来也比较乱,以上代码还没有做查询数据是的子线程优化等等,希望朋友们看到代码给我指点
再就是我还在寻找,解压rar文件,再就是打开ppt格式文件,还有就是cad制图,懂这方面的大神看到的联系我。







    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值