程序员代码改变世界java

工作总结|文件系统模型的三种实现

2016-11-18  本文已影响189人  寒石

最近在做关于压缩文件预览的需求,涉及到对于文件系统的目录结构进行简单的模拟。在实现过程中,尝试了三种实现方式,下文对三种实现方式做详细说明。

传统目录结构

使用递归构建文件二叉树结构

public class CompressedFileInfo {
    public String filePath;
    public String parentPath;
    private String name;
    public boolean isDirectory;
    private int levels;
    private CompressedFileInfo leftFriendInfo;
    private CompressedFileInfo rightChildInfo;
}
  public void buildFileInfo(CompressedFileInfo root) {
          if (root == null) {
              return;
          }
          if (root.levels == 0) {
              if (root.rightChildInfo == null) {
                  root.rightChildInfo = this;
                  return;
              } else {
                  root = root.rightChildInfo;
              }
          }
          if (root.levels < levels) {
              if (filePath.startsWith(root.filePath)) {
                  if (root.rightChildInfo == null) {
                      root.rightChildInfo = this;
                  } else {
                      buildFileInfo(root.rightChildInfo);
                  }
              } else {
                  buildFileInfo(root.leftFriendInfo);
              }
          } else if (root.levels == levels) {
              if (parentPath == null || filePath.startsWith(root.filePath)) {
                  if (root.leftFriendInfo == null) {
                      root.leftFriendInfo = this;
                  } else {
                      buildFileInfo(root.leftFriendInfo);
                  }
              } else {
                  Log.d("raytest", "Build Error");
              }
          } else {
              Log.e("raytest", "Build Error");
          }
  }
二叉树目录结构

使用递归构建文件树形结构

public class DecompressFileItem extends FileItem {
    private String mParentPath;
    private List<FileItem> mChildList;
}

此处的FileItem可以理解为File对象属性的一些映射,包含一些获取文件名,获取文件路径之类的常用属性和方法。

public void buildFileSys(Map<String, FileItem> nodeMap, DecompressFileItem paramInfo, boolean isRoot) {
        Map<String, FileItem> tempMap = new HashMap<>();
        tempMap.putAll(nodeMap);
        if (paramInfo == null || TextUtils.isEmpty(paramInfo.mData)) {
            return;
        }
        if (nodeMap == null || nodeMap.size() < 1) {
            return;
        }
        String paramPathKey = paramInfo.mData;
        if (isRoot) {
            paramPathKey = paramPathKey.substring(0, paramPathKey.lastIndexOf(File.separator));
            tempMap.remove(paramPathKey);
        } else {
            tempMap.remove(paramInfo.mData);
        }
        for (Map.Entry<String, FileItem> entry : tempMap.entrySet()) {
            DecompressFileItem item = (DecompressFileItem) entry.getValue();
            if (paramPathKey.equals(item.getParentPath())) {
                if (paramInfo.mChildList == null) {
                    paramInfo.mChildList = new ArrayList<>();
                }
                paramInfo.mChildList.add(item);
                item.buildFileSys(tempMap, item, false);
            }
        }
    }

使用迭代创建文件树形结构

/**
 * called by root to create the file struct
 * @param nodeMap
 */
public void buildFileSys(Map<String, FileItem> nodeMap) {
    if (nodeMap == null || nodeMap.size() < 1) {
        return;
    }
    for (Map.Entry<String, FileItem> entrySource : nodeMap.entrySet()) {
        String pathKey = entrySource.getKey();
        if (TextUtils.isEmpty(pathKey)) {
            continue;
        }
        DecompressFileItem pathItem = (DecompressFileItem) entrySource.getValue();
        if (pathItem == null) {
            continue;
        }
        for (Map.Entry<String, FileItem> entryCompare : nodeMap.entrySet()) {
            DecompressFileItem compareItem = (DecompressFileItem) entryCompare.getValue();
            if (compareItem == null) {
                continue;
            }
            boolean isRootNode = compareItem.mData.equals(mData) && compareItem.mFileName.equals(mFileName);
            if (pathKey.equals(compareItem.getParentPath()) && !isRootNode) {
                if (pathItem.mChildList == null) {
                    pathItem.mChildList = new ArrayList<FileItem>();
                }
                pathItem.mChildList.add(compareItem);
                // build file count
                if (pathItem.mIsDir) {
                    pathItem.mFileCount++;
                }
            }
        }
    }
}

最后,附上银魂的一句吐槽:压力是导致秃顶的原因,所以请注意不要压力太大,但这样一来反而容易堆积压力,所以归根到底我们无能为力」

上一篇 下一篇

猜你喜欢

热点阅读