/// <summary> /// 把pathToPut的路径放入到Apk中的pathInApk路径,如果已经存在则覆盖。 /// </summary> /// <param name="inApkPath">输入的apk</param> /// <param name="outApkPath">输出的apk</param> /// <param name="pathInApk">apk中的路径</param> /// <param name="pathToPut">需要被放置的路径</param> public static void PutPathInAPK(string inApkPath, string outApkPath, string pathInApk, string pathToPut) { InitInner(); var apkDir = SpaceUtil.NewTempPath(); Logger.Log($"Unpack apk to '{apkDir}'..."); UnpackAPK(inApkPath, apkDir); // 放 Logger.Log($"Copy '{pathToPut}' to '{Path.Combine(apkDir, pathInApk)}'..."); PathUtil.CopyPath(pathToPut, Path.Combine(apkDir, pathInApk), true); Logger.Log($"Pack apk to '{outApkPath}'..."); PackAPK(apkDir, outApkPath); }
/// <summary> /// 拷贝源路径到目标路径 /// </summary> /// <param name="sourPath"> 源路径 </param> /// <param name="destPath"> 目标路径 </param> /// <param name="force"> 是否强制覆盖目标路径 </param> public static void CopyPath(string sourPath, string destPath, bool force) { if (sourPath == destPath) { throw new DoodleException("sourPath and destPath can't be same!"); } int wildcardIndex = sourPath.LastIndexOf("*", System.StringComparison.Ordinal); bool srcWildcard = wildcardIndex >= 0; string needCheckPath = sourPath; if (srcWildcard) { int lastSlashIndex = sourPath.LastIndexOf('/'); if (lastSlashIndex > wildcardIndex) { throw new DoodleException(string.Format("sourPath {0} 是不合法的路径!", sourPath)); } needCheckPath = Path.GetDirectoryName(sourPath); } var sourState = GetPathState(needCheckPath); if (sourState == PathState.None) { throw new DoodleException(string.Format("{0} 路径不存在!", needCheckPath)); } var destState = GetPathState(destPath); if (!force && destState != PathState.None) { throw new DoodleException("destPath is not empty!"); } if (srcWildcard) { if (destState == PathState.File) { if (force) { PathUtil.RemovePath(destPath); } else { throw new DoodleException(string.Format("destPath {0} 不能是一个文件,除非指明force!", destPath)); } } var files = Directory.GetFiles(Path.GetDirectoryName(sourPath), Path.GetFileName(sourPath)); DirUtil.TryCreateDir(destPath); foreach (var f in files) { PathUtil.CopyPath(f, Path.Combine(destPath, Path.GetFileName(f)), force); } } else { var relation = GetPathRelation(sourPath, destPath); if (sourState == PathState.Dir && relation == PathRelation.Parent) { throw new DoodleException("can't copy a directory into itself!"); } if (destState == PathState.Dir && relation == PathRelation.Child) { throw new DoodleException("can't overwrite sourPath's parent dir!"); } DirUtil.TryCreateParentDir(destPath, force); TryRemovePath(destPath); if (sourState == PathState.File) { File.Copy(sourPath, destPath); } else if (sourState == PathState.Dir) { DirUtil.CopyDir(sourPath, destPath, true); } } }