srikanth-lingala / zip4j

A Java library for zip files and streams
Apache License 2.0
2.08k stars 313 forks source link

Get the path of compressed files in Recycler View #537

Open appt2 opened 8 months ago

appt2 commented 8 months ago

Screenshot_2024-02-18-18-31-03-623_Ninja coder Ghostemane code


package Ninja.coder.Ghostemane.code.adapter;

import Ninja.coder.Ghostemane.code.R;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.LinearLayout;
import android.widget.ImageView;
import android.view.LayoutInflater;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
import net.lingala.zip4j.model.FileHeader;

public class ZipListFileShowAd extends RecyclerView.Adapter<ZipListFileShowAd.VH> {

  protected List<FileHeader> listModel;

  public ZipListFileShowAd(List<FileHeader> listModel) {
    this.listModel = listModel;
  }

  @Override
  public int getItemCount() {
    return listModel.size();
  }

  @Override
  public void onBindViewHolder(VH viewHolder, int pos) {

    View view = viewHolder.itemView;
    RecyclerView.LayoutParams _lp =
        new RecyclerView.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    view.setLayoutParams(_lp);
    FileHeader fileM = listModel.get(pos);
    viewHolder.tvTools.setVisibility(View.GONE);
    viewHolder.folderName.setText(fileM.getFileName());
  }

  @Override
  public VH onCreateViewHolder(ViewGroup parnt, int pos) {
    View view =
        LayoutInflater.from(parnt.getContext()).inflate(R.layout.folder_remster, parnt, false);
    RecyclerView.LayoutParams _lp =
        new RecyclerView.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    view.setLayoutParams(_lp);
    return new VH(view);
  }

  public class VH extends RecyclerView.ViewHolder {
    protected TextView folderName, tvTools;
    protected LinearLayout roots;
    protected ImageView icon;

    public VH(View view) {
      super(view);
      folderName = view.findViewById(R.id.folderName);
      tvTools = view.findViewById(R.id.tvTools);
      roots = view.findViewById(R.id.roots);
      icon = view.findViewById(R.id.icon);
    }
  }
}

Hello, I use your library to get the list of compressed files, but as you can see in the picture, the path of the files is displayed strangely, what is the solution?

oleg-cherednik commented 7 months ago

Zip files contains file names with paths. The real file name is the substring after the last '/'. Your code treats output entry list incorrectly.

appt2 commented 7 months ago

Zip files contains file names with paths. The real file name is the substring after the last '/'. Your code treats output entry list incorrectly.

So how can I fix this problem?

oleg-cherednik commented 7 months ago

When fileName ends with / - this is a folder. I.e. zipEntry is not a zip file but a folder. When fileName does not end with '/' and contain / - it means that this zipEntry is a file with given path (including subfolders). Just remember, that zip file contains a list of zipEntry. Each zipEntry can be either a file or a folder, i.e. a file with zero length.

appt2 commented 7 months ago

When fileName ends with / - this is a folder. I.e. zipEntry is not a zip file but a folder. When fileName does not end with '/' and contain / - it means that this zipEntry is a file with given path (including subfolders)

I did not understand, can you give a code example?

oleg-cherednik commented 7 months ago

When fileName ends with / - this is a folder. I.e. zipEntry is not a zip file but a folder. When fileName does not end with '/' and contain / - it means that this zipEntry is a file with given path (including subfolders)

I did not understand, can you give a code example?

Give me a link to your zip file

appt2 commented 7 months ago

When fileName ends with / - this is a folder. I.e. zipEntry is not a zip file but a folder. When fileName does not end with '/' and contain / - it means that this zipEntry is a file with given path (including subfolders)

I did not understand, can you give a code example?

Give me a link to your zip file

I don't have a zip file right now