basePath的好处

首先在谈到basePath之前,我想先来讨论一下相对路径与绝对路径的区别。

相对路径-以引用文件之网页所在位置为参考基础,而建立出的目录路径。
绝对路径-以Web站点根目录为参考基础的目录路径。

其实绝对路径与相对路径的不同处,只在于描述目录路径时,所采用的参考点不同。由于对网站上所有文件而言,根目录这个参考点对所有文件都是一样的,因此,运用以根目录为参考点的路径描述方式才会被称之为绝对路径。

在这里需要讲几个特殊符号:
"/"代表根目录,".."代表上一层目录,而"../../"所代表的是上一层目录的上一层目录。假设你注册了域名www.blabla.cn,并申请了虚拟主机,你的虚拟主机提供商会给你一个目录,比如www,这个www就是你网站的根目录。

假设你在www根目录下放了一个文件index.html,这个文件的绝对路径就是: http://www.blabla.cn/index.html。

假设你在www根目录下建了一个目录叫html_tutorials,然后在该目录下放了一个文件index.html,这个文件的绝对路径就是http://www.blabla.cn/html_tutorials/index.html

了解了相对路径与绝对路劲的概念后,我们来看看所谓的bassPath是怎么写的:
String path = request.getContextPath();
String basePath = request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort()+ path + "/";

看看上面的代码,我们来了解一下它具体的意思,
request.getContextPath()是在开发Web项目时,经常用到的方法,其作用是获取当前的系统路径。request.getContextPath()得到项目名,如果项目为根目录,则得到一个"",即空的字条串, 如果项目为Test, <%=request.getContextPath()% >/ 将得到Test/,服务器端的路径则会自动加上.

request.getScheme()返回的协议名称,默认是http。
request.getServerName()返回的是你浏览器中显示的主机名(例127.0.0.1).
getServerPort()获取服务器端口号(例:8080)。
我们不防把basePath打印出来看看是什么,http://127.0.0.1:8080/Test。

值得注意的是:request.getServerName()始终获取的是浏览器中显示的主机名,比如可以举个例子,我们在浏览其中输入的主机名是本地主机名192.168.1.4,那么我们在内网中可以通过http://192.168.1.4:8080/Test这个路径来访问项目,如果是外网需要访问则需要提供域名http://www.blabla.cn/Test来访问,当然内网也可以通过这个路径来访问,此时打印出来的basePath是这个路径http://www.blabla.cn:8080/Test了,而不是http://192.168.1.4:8080/Test

在开发中试用basePath能够很好的避免出现路径的问题。
package service import ( "fmt" "log" "os" "regexp" "sort" "strconv" "strings" "unicode/utf16" "unicode/utf8" "github.com/tidwall/gjson" "github.com/tidwall/sjson" ) // JSONProcessor 处理 JSON 数据的工具类 type JSONProcessor struct { jsonStr string } // NewJSONProcessor 创建新的 JSON 处理器 func NewJSONProcessor(jsonStr string) *JSONProcessor { return &JSONProcessor{jsonStr: jsonStr} } // RemoveInvalidImage 删除不合规图片及其关联数据 func (jp *JSONProcessor) RemoveInvalidImage(invalidURL string) error { // 1. 检查并删除 spec_detail 中的规格值 if err := jp.removeSpecValuesByImage(invalidURL); err != nil { return err } // 2. 删除其他图片字段中的无效图片 fields := []string{"white_background_pic", "main_image_three_to_four", "pic"} for _, field := range fields { if err := jp.removeImageFromField(field, invalidURL); err != nil { return fmt.Errorf("failed to remove from %s: %v", field, err) } } // 3. 从描述中删除图片 if err := jp.removeImageFromDescription(invalidURL); err != nil { return fmt.Errorf("failed to remove from description: %v", err) } return nil } // removeSpecValuesByImage 删除包含指定图片URL的规格值 func (jp *JSONProcessor) removeSpecValuesByImage(invalidURL string) error { basePath := "schema.model" specDetailPath := fmt.Sprintf("%s.spec_detail.value", basePath) result := gjson.Get(jp.jsonStr, specDetailPath) if !result.Exists() { return nil // 没有规格数据,直接返回 } // 收集需要删除的规格值ID var specValueIDs []string result.ForEach(func(_, group gjson.Result) bool { group.Get("spec_values").ForEach(func(_, spec gjson.Result) bool { if imgURL := spec.Get("img_url").String(); imgURL == invalidURL { if id := spec.Get("id").String(); id != "" { specValueIDs = append(specValueIDs, id) } } return true }) return true }) // 删除所有关联的规格值 for _, id := range specValueIDs { if err := jp.RemoveSpecValueByID(id); err != nil { return fmt.Errorf("failed to remove spec value %s: %v", id, err) } } return nil } // removeImageFromField 从指定图片字段中删除无效图片 func (jp *JSONProcessor) removeImageFromField(fieldPath, invalidURL string) error { fullPath := fmt.Sprintf("schema.model.%s.value", fieldPath) result := gjson.Get(jp.jsonStr, fullPath) if !result.Exists() { return nil // 字段不存在,直接返回 } // 收集需要删除的索引 var indicesToDelete []int result.ForEach(func(index gjson.Result, item gjson.Result) bool { if url := item.Get("url").String(); url == invalidURL { indicesToDelete = append(indicesToDelete, int(index.Int())) } return true }) // 从后往前删除(避免索引变化) sort.Sort(sort.Reverse(sort.IntSlice(indicesToDelete))) for _, idx := range indicesToDelete { path := fmt.Sprintf("%s.%d", fullPath, idx) var err error jp.jsonStr, err = sjson.Delete(jp.jsonStr, path) if err != nil { return err } } return nil } // unescapeUnicode 解码 Unicode 转义序列(如 \u003c -> <) func unescapeUnicode(s string) string { var buf strings.Builder reader := strings.NewReader(s) // 使用不同的变量名避免冲突 for { r, _, err := reader.ReadRune() // 读取原始字符 if err != nil { break } if r == '\\' { next, _, _ := reader.ReadRune() // 读取下一个字符 if next == 'u' { // 读取 4 位十六进制 hex := make([]rune, 4) for i := 0; i < 4; i++ { ch, _, _ := reader.ReadRune() hex[i] = ch } // 转换为 rune if code, err := strconv.ParseInt(string(hex), 16, 32); err == nil { buf.WriteRune(rune(code)) continue } buf.WriteRune('\\') buf.WriteRune('u') buf.WriteString(string(hex)) continue } buf.WriteRune('\\') buf.WriteRune(next) continue } buf.WriteRune(r) } return buf.String() } // escapeUnicode 将特殊字符编码为 Unicode 转义序列 func escapeUnicode(s string) string { var buf strings.Builder for _, r := range s { if r <= 0x7F { // ASCII 字符 switch r { case '"', '\\', '<', '>', '&': buf.WriteString(fmt.Sprintf("\\u%04X", r)) default: buf.WriteRune(r) } } else { // 非 ASCII 字符 if utf8.ValidRune(r) { buf.WriteRune(r) } else { // 处理代理对(surrogate pairs) for _, surr := range utf16.Encode([]rune{r}) { buf.WriteString(fmt.Sprintf("\\u%04X", surr)) } } } } return buf.String() } // removeImageFromDescription 从描述中删除无效图片(处理 Unicode 转义) func (jp *JSONProcessor) removeImageFromDescription(invalidURL string) error { descPath := "schema.model.description.value" result := gjson.Get(jp.jsonStr, descPath) if !result.Exists() { return nil } // 1. 获取并解码 Unicode 转义的 HTML escapedHTML := result.String() decodedHTML := unescapeUnicode(escapedHTML) // 2. 处理图片删除 escapedURL := regexp.QuoteMeta(invalidURL) re := regexp.MustCompile( `<img\s[^>]*?src\s*=\s*["']` + escapedURL + `["'][^>]*>`, ) newHTML := re.ReplaceAllString(decodedHTML, "") // 3. 重新编码为 Unicode 转义序列 reencodedHTML := escapeUnicode(newHTML) // 4. 使用 SetRaw 避免二次编码 var err error jp.jsonStr, err = sjson.SetRaw(jp.jsonStr, descPath, strconv.Quote(reencodedHTML)) return err } // ExtractAllPics 提取所有图片 URL(包括描述中的图片) func (jp *JSONProcessor) ExtractAllPics() []string { // 基础路径 basePath := "schema.model" // 存储所有图片的集合(使用 map 去重) uniquePics := make(map[string]bool) // 1. 处理指定字段的图片 fields := []string{"spec_detail", "white_background_pic", "main_image_three_to_four", "pic"} for _, field := range fields { path := fmt.Sprintf("%s.%s.value", basePath, field) result := gjson.Get(jp.jsonStr, path) if result.Exists() { result.ForEach(func(_, item gjson.Result) bool { // 处理 spec_detail 的特殊结构 if field == "spec_detail" { item.Get("spec_values").ForEach(func(_, spec gjson.Result) bool { if img := spec.Get("img_url"); img.Exists() { uniquePics[img.String()] = true } return true }) } else if url := item.Get("url"); url.Exists() { // 处理其他标准字段 uniquePics[url.String()] = true } return true }) } } // 2. 处理 description 中的图片 descPath := fmt.Sprintf("%s.description.value", basePath) descResult := gjson.Get(jp.jsonStr, descPath) if descResult.Exists() { html := descResult.String() re := regexp.MustCompile(`<img[^>]+src="([^">]+)"`) matches := re.FindAllStringSubmatch(html, -1) for _, match := range matches { if len(match) > 1 && !uniquePics[match[1]] { uniquePics[match[1]] = true } } } // 转换为切片返回 allPics := make([]string, 0, len(uniquePics)) for pic := range uniquePics { allPics = append(allPics, pic) } return allPics } // RemoveSpecValueByID 根据 spec_value ID 删除规格值及其关联的 SKU func (jp *JSONProcessor) RemoveSpecValueByID(specValueID string) error { basePath := "schema.model" // 1. 删除 spec_detail 中的 spec_value specDetailPath := fmt.Sprintf("%s.spec_detail.value", basePath) specDetailResult := gjson.Get(jp.jsonStr, specDetailPath) if !specDetailResult.Exists() { return fmt.Errorf("spec_detail.value does not exist") } specGroups := specDetailResult.Array() // 收集需要删除的索引(组索引和值索引) type deletionPoint struct { groupIndex int valueIndex int } var deletions []deletionPoint // 遍历所有规格组 for groupIdx, group := range specGroups { specValues := group.Get("spec_values").Array() // 遍历组内的规格值 for valueIdx, value := range specValues { if id := value.Get("id").String(); id == specValueID { deletions = append(deletions, deletionPoint{groupIdx, valueIdx}) } } } // 从后往前删除(避免索引变化) sort.Slice(deletions, func(i, j int) bool { if deletions[i].groupIndex == deletions[j].groupIndex { return deletions[i].valueIndex > deletions[j].valueIndex } return deletions[i].groupIndex > deletions[j].groupIndex }) for _, del := range deletions { path := fmt.Sprintf("%s.%d.spec_values.%d", specDetailPath, del.groupIndex, del.valueIndex) var err error jp.jsonStr, err = sjson.Delete(jp.jsonStr, path) if err != nil { return err } } // 2. 删除关联的 SKU skuDetailPath := fmt.Sprintf("%s.sku_detail.value", basePath) skuDetailResult := gjson.Get(jp.jsonStr, skuDetailPath) if !skuDetailResult.Exists() { // 没有 SKU 数据,直接返回 return nil } skus := skuDetailResult.Array() var skuIndicesToDelete []int // 收集需要删除的 SKU 索引 for idx, sku := range skus { specDetailIDs := sku.Get("spec_detail_ids").Array() for _, id := range specDetailIDs { if id.String() == specValueID { skuIndicesToDelete = append(skuIndicesToDelete, idx) break // 找到匹配即跳出 } } } // 从后往前删除 SKU(避免索引变化) sort.Sort(sort.Reverse(sort.IntSlice(skuIndicesToDelete))) for _, idx := range skuIndicesToDelete { path := fmt.Sprintf("%s.%d", skuDetailPath, idx) var err error jp.jsonStr, err = sjson.Delete(jp.jsonStr, path) if err != nil { return err } } return nil } // UpdateSkuPrice 更新 SKU 的价格 func (jp *JSONProcessor) UpdateSkuPrice(skuID, newPrice string) error { path := "schema.model.sku_detail.value" result := gjson.Get(jp.jsonStr, path) if !result.Exists() { return fmt.Errorf("sku_detail.value does not exist") } skuArray := result.Array() for idx, sku := range skuArray { if id := sku.Get("id").String(); id == skuID { fullPath := fmt.Sprintf("%s.%d.price", path, idx) var err error jp.jsonStr, err = sjson.Set(jp.jsonStr, fullPath, newPrice) return err } } return fmt.Errorf("SKU with ID %s not found", skuID) } // UpdateSkuStock 更新 SKU 的库存 func (jp *JSONProcessor) UpdateSkuStock(skuID string, newStock int) error { path := "schema.model.sku_detail.value" result := gjson.Get(jp.jsonStr, path) if !result.Exists() { return fmt.Errorf("sku_detail.value does not exist") } skuArray := result.Array() for idx, sku := range skuArray { if id := sku.Get("id").String(); id == skuID { // 更新 stock_info.stock_num stockPath := fmt.Sprintf("%s.%d.stock_info.stock_num", path, idx) var err error jp.jsonStr, err = sjson.Set(jp.jsonStr, stockPath, newStock) if err != nil { return err } // 更新 self_sell_stock selfStockPath := fmt.Sprintf("%s.%d.self_sell_stock", path, idx) jp.jsonStr, err = sjson.Set(jp.jsonStr, selfStockPath, newStock) return err } } return fmt.Errorf("SKU with ID %s not found", skuID) } // GetField 获取指定字段的值 func (jp *JSONProcessor) GetField(fieldPath string) (gjson.Result, bool) { fullPath := fmt.Sprintf("schema.model.%s", fieldPath) result := gjson.Get(jp.jsonStr, fullPath) return result, result.Exists() } // UpdateField 更新指定字段的值 func (jp *JSONProcessor) UpdateField(fieldPath string, newValue interface{}) error { fullPath := fmt.Sprintf("schema.model.%s", fieldPath) newJSON, err := sjson.Set(jp.jsonStr, fullPath, newValue) if err != nil { return err } jp.jsonStr = newJSON return nil } // DeleteField 删除指定字段 func (jp *JSONProcessor) DeleteField(fieldPath string) error { fullPath := fmt.Sprintf("schema.model.%s", fieldPath) newJSON, err := sjson.Delete(jp.jsonStr, fullPath) if err != nil { return err } jp.jsonStr = newJSON return nil } // AddArrayItem 向数组字段添加新元素 func (jp *JSONProcessor) AddArrayItem(fieldPath string, newItem interface{}) error { // 检查数组字段是否存在 basePath := fmt.Sprintf("schema.model.%s.value", fieldPath) result := gjson.Get(jp.jsonStr, basePath) // 如果数组不存在,先创建空数组 if !result.Exists() { var err error jp.jsonStr, err = sjson.Set(jp.jsonStr, basePath, []interface{}{}) if err != nil { return fmt.Errorf("failed to create array field: %v", err) } } // 添加新元素到数组末尾 fullPath := fmt.Sprintf("%s.-1", basePath) newJSON, err := sjson.Set(jp.jsonStr, fullPath, newItem) if err != nil { return fmt.Errorf("failed to add array item: %v", err) } jp.jsonStr = newJSON return nil } // RemoveArrayItem 从数组中删除指定索引的元素 func (jp *JSONProcessor) RemoveArrayItem(fieldPath string, index int) error { basePath := fmt.Sprintf("schema.model.%s.value", fieldPath) result := gjson.Get(jp.jsonStr, basePath) if !result.Exists() { return fmt.Errorf("array field does not exist") } // 检查索引是否有效 array := result.Array() if index < 0 || index >= len(array) { return fmt.Errorf("index %d out of range [0, %d]", index, len(array)-1) } fullPath := fmt.Sprintf("%s.%d", basePath, index) newJSON, err := sjson.Delete(jp.jsonStr, fullPath) if err != nil { return err } jp.jsonStr = newJSON return nil } // GetJSON 获取当前处理后的 JSON func (jp *JSONProcessor) GetJSON() string { return jp.jsonStr } // TestFunc 测试函数 func TestFunc() { content, err := os.ReadFile("asyncCheckPost.json") if err != nil { log.Printf("读取过滤文件失败: %v", err) return } processor := NewJSONProcessor(string(content)) // 提取所有图片 allPics := processor.ExtractAllPics() fmt.Printf("提取到 %d 张图片\n", len(allPics)) // 假设我们检测到第二张图片不合规 if len(allPics) > 1 { invalidURL := "https://p3-aio.ecombdimg.com/obj/ecom-shop-material/jpeg_m_a469f301b9c195b6b833240f9adc1ffc_sx_36996_www790-314" fmt.Printf("检测到不合规图片: %s\n", invalidURL) // 删除不合规图片及其关联数据 if err := processor.RemoveInvalidImage(invalidURL); err != nil { fmt.Println("删除不合规图片失败:", err) } else { fmt.Println("成功删除不合规图片及其关联数据") // 验证删除后图片数量 newPics := processor.ExtractAllPics() fmt.Printf("删除后剩余图片: %d 张\n", len(newPics)) // 检查是否不再包含不合规图片 for _, url := range newPics { if url == invalidURL { fmt.Println("错误: 不合规图片仍然存在!") break } } } } // 1. 删除规格值及其关联的 SKU specValueID := "1839581026529404" // 小号钢架尺30米的 ID if err := processor.RemoveSpecValueByID(specValueID); err != nil { fmt.Println("删除规格值失败:", err) } else { fmt.Println("成功删除规格值及其关联SKU") } // 2. 更新 SKU 价格和库存 skuID := "3527608283404802" // 第一个 SKU 的 ID newPrice := "99.99" newStock := 3000 if err := processor.UpdateSkuPrice(skuID, newPrice); err != nil { fmt.Println("更新价格失败:", err) } else { fmt.Println("成功更新SKU价格") } if err := processor.UpdateSkuStock(skuID, newStock); err != nil { fmt.Println("更新库存失败:", err) } else { fmt.Println("成功更新SKU库存") } // 3. 测试数组操作 // 向不存在的数组添加元素 if err := processor.AddArrayItem("new_field", map[string]string{"test": "value"}); err != nil { fmt.Println("添加数组项失败:", err) } else { fmt.Println("成功添加数组项到新字段") } // 尝试删除不存在的数组项 if err := processor.RemoveArrayItem("non_existent_field", 0); err != nil { fmt.Println("删除数组项失败(预期):", err) } else { fmt.Println("意外成功删除不存在的数组项") } // 尝试越界删除 if err := processor.RemoveArrayItem("pic", 100); err != nil { fmt.Println("删除数组项失败(预期):", err) } else { fmt.Println("意外成功删除越界数组项") } // 获取最终 JSON // finalJSON := processor.GetJSON() // fmt.Println(finalJSON) } 这段代码中,请帮我重构一下,因为schema.model 的路径有可能为 data.model,需要根据json数据来判断。
08-08
package com.kotei.overseas.navi.update; import android.util.Log; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.Arrays; import java.util.Enumeration; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.atomic.AtomicLong; import java.util.zip.Deflater; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import java.util.zip.ZipOutputStream; public class FileUtils { private static final String TAG = "FileUtils"; public static final int BUFFER_SIZE = 10 * 1024 * 1024; // 10MB缓冲区 private static long totalSize = 0; private static long processedSize = 0; private static long lastCheckTime = 0; /** * 带进度压缩目录 * * @param sourceDir 源目录 * @param outputFile 输出文件 * @param callback 进度回调 * @return 是否成功 */ public static boolean compressDirectoryWithProgress(File sourceDir, File outputFile, USBOfflineUpdater.ProgressCallback callback) { // 检查参数是否为 null if (sourceDir == null) { throw new IllegalArgumentException("Source directory cannot be null"); } if (outputFile == null) { throw new IllegalArgumentException("Output file cannot be null"); } totalSize = getDirectorySize(sourceDir); try (FileOutputStream fos = new FileOutputStream(outputFile); ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(fos))) { zos.setLevel(Deflater.BEST_SPEED); // 压缩目录 boolean compressResult = zipSubDirectoryWithProgress("", sourceDir, zos, callback, true); // 3. 压缩完成后删除原始数据 if (compressResult) { Log.i(TAG, "压缩成功,开始删除原始数据..."); return deleteDirectoryContents(sourceDir); } return false; } catch (IOException e) { Log.e(TAG, "压缩目录失败", e); return false; } } /** * 带进度压缩子目录 * * @param basePath 基础路径 * @param dir 当前目录 * @param zos Zip输出流 * @param callback 进度回调 * @param deleteAfterAdd 添加后是否删除文件 * @return 是否成功 */ private static boolean zipSubDirectoryWithProgress(String basePath, File dir, ZipOutputStream zos, USBOfflineUpdater.ProgressCallback callback, boolean deleteAfterAdd) throws IOException { // 检查参数是否为 null if (dir == null) { throw new IllegalArgumentException("Directory cannot be null"); } if (zos == null) { throw new IllegalArgumentException("ZipOutputStream cannot be null"); } byte[] buffer = new byte[BUFFER_SIZE]; File[] files = dir.listFiles(); if (files == null) return true; // 按文件大小排序,先处理大文件 Arrays.sort(files, (f1, f2) -> { if (f1.isDirectory() && !f2.isDirectory()) return -1; if (!f1.isDirectory() && f2.isDirectory()) return 1; return Long.compare(f2.length(), f1.length()); // 大文件优先 }); for (File file : files) { if (Thread.currentThread().isInterrupted()) { return false; } String path = basePath + file.getName(); if (file.isDirectory()) { // 递归处理子目录 if (!zipSubDirectoryWithProgress(path + File.separator, file, zos, callback, deleteAfterAdd)) { return false; } // 删除空目录 if (deleteAfterAdd && file.listFiles().length == 0) { if (!file.delete()) { Log.w(TAG, "删除空目录失败: " + file.getAbsolutePath()); } } } else { try (FileInputStream fis = new FileInputStream(file); BufferedInputStream bis = new BufferedInputStream(fis)) { zos.putNextEntry(new ZipEntry(path)); int length; while ((length = bis.read(buffer)) > 0) { zos.write(buffer, 0, length); processedSize += length; // 更新进度 if (callback != null) { callback.onProgress(processedSize, totalSize); } } zos.closeEntry(); // 压缩后立即删除文件 if (deleteAfterAdd) { if (!file.delete()) { Log.w(TAG, "删除文件失败: " + file.getAbsolutePath()); } else { Log.d(TAG, "已删除: " + file.getAbsolutePath()); } } } catch (InterruptedException e) { throw new RuntimeException(e); } } } return true; } /** * 计算目录大小 * * @param directory 目录 * @return 目录大小(字节) */ public static long getDirectorySize(File directory) { long size = 0; if (directory == null || !directory.exists()) return 0; if (directory.isDirectory()) { File[] files = directory.listFiles(); if (files != null) { for (File file : files) { size += getDirectorySize(file); } } } else { size = directory.length(); } return size; } // 静态内部类:解压任务 private static class ExtractTask implements Callable<Long> { private final ZipFile zip; private final ZipEntry entry; private final File file; private final USBOfflineUpdater.ProgressCallback callback; private final long totalSize; public ExtractTask(ZipFile zip, ZipEntry entry, File file, USBOfflineUpdater.ProgressCallback callback, long totalSize) { this.zip = zip; this.entry = entry; this.file = file; this.callback = callback; this.totalSize = totalSize; } @Override public Long call() throws IOException { long entrySize = 0; try (InputStream in = zip.getInputStream(entry); FileOutputStream fos = new FileOutputStream(file); BufferedOutputStream bos = new BufferedOutputStream(fos, 128 * 1024)) { byte[] buffer = new byte[128 * 1024]; int length; while ((length = in.read(buffer)) > 0) { bos.write(buffer, 0, length); entrySize += length; // 定期报告进度(每5MB) if (callback != null && entrySize % (5 * 1024 * 1024) == 0) { synchronized (FileUtils.class) { callback.onProgress(entrySize, totalSize); } } } bos.flush(); } catch (IOException e) { Log.e(TAG, "解压文件失败: " + file.getName(), e); throw e; } catch (InterruptedException e) { throw new RuntimeException(e); } return entrySize; } } /** * 带进度解压ZIP文件 * * @param zipFile ZIP文件 * @param targetDir 目标目录 * @param callback 进度回调 * @return 是否成功 */ public static boolean extractZipWithProgress(File zipFile, File targetDir, USBOfflineUpdater.ProgressCallback callback) { long totalSize = zipFile.length(); AtomicLong processedSize = new AtomicLong(0); // 使用原子类型保证线程安全 // 创建线程池(4线程) ExecutorService executor = Executors.newFixedThreadPool(4); List<Future<Long>> futures = new ArrayList<>(); try (ZipFile zip = new ZipFile(zipFile)) { // 第一遍:预创建所有目录 Map<String, Boolean> createdDirs = new HashMap<>(); Enumeration<? extends ZipEntry> entries = zip.entries(); while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); if (entry.isDirectory()) { File dir = new File(targetDir, entry.getName()); if (!dir.exists() && !dir.mkdirs()) { Log.w(TAG, "创建目录失败: " + dir.getAbsolutePath()); } createdDirs.put(entry.getName(), true); } } // 第二遍:并行解压文件 entries = zip.entries(); while (entries.hasMoreElements()) { final ZipEntry entry = entries.nextElement(); if (entry.isDirectory()) continue; // 确保父目录存在 File file = new File(targetDir, entry.getName()); File parent = file.getParentFile(); if (parent != null && !parent.exists()) { if (!parent.mkdirs()) { Log.w(TAG, "创建父目录失败: " + parent.getAbsolutePath()); } } // 提交解压任务 futures.add(executor.submit(new ExtractTask( zip, entry, file, callback, totalSize ))); } // 等待所有任务完成并统计进度 for (Future<Long> future : futures) { try { long size = future.get(); processedSize.addAndGet(size); // 更新总进度 if (callback != null) { callback.onProgress(processedSize.get(), totalSize); } } catch (ExecutionException e) { Log.e(TAG, "解压任务失败", e.getCause()); } } return true; } catch (IOException | InterruptedException e) { Log.e(TAG, "解压失败: " + zipFile.getName(), e); return false; } finally { executor.shutdownNow(); } } /** * 带进度拷贝文件 * * @param src 源文件 * @param dest 目标文件 * @param callback 进度回调 * @return 是否成功 */ public static boolean copyFileWithProgress(File src, File dest, USBOfflineUpdater.ProgressCallback callback) { long totalSize = src.length(); long copiedSize = 0; try (InputStream in = new BufferedInputStream(new FileInputStream(src)); OutputStream out = new BufferedOutputStream(new FileOutputStream(dest))) { byte[] buffer = new byte[BUFFER_SIZE]; int length; while ((length = in.read(buffer)) > 0) { if (Thread.currentThread().isInterrupted()) { return false; } out.write(buffer, 0, length); copiedSize += length; // 优化后的检查逻辑(每秒最多1次) long currentTime = System.currentTimeMillis(); if (currentTime - lastCheckTime > 1000) { if (Thread.currentThread().isInterrupted()) { return false; } if (USBOfflineUpdater.getInstance().isCancelled) { return false; } // 更新进度 if (callback != null) { callback.onProgress(copiedSize, totalSize); } lastCheckTime = currentTime; } } return true; } catch (IOException e) { Log.e(TAG, "拷贝失败: " + src.getName(), e); return false; } catch (InterruptedException e) { throw new RuntimeException(e); } } /** * 压缩目录 * * @param sourceDir 源目录 * @param outputFile 输出文件 * @return 是否成功 */ public static boolean compressDirectory(File sourceDir, File outputFile) { try (FileOutputStream fos = new FileOutputStream(outputFile); ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(fos))) { zipSubDirectory("", sourceDir, zos); return true; } catch (IOException e) { Log.e(TAG, "压缩目录失败", e); return false; } } private static void zipSubDirectory(String basePath, File dir, ZipOutputStream zos) throws IOException { byte[] buffer = new byte[BUFFER_SIZE]; File[] files = dir.listFiles(); if (files == null) return; for (File file : files) { String path = basePath + file.getName(); if (file.isDirectory()) { zipSubDirectory(path + File.separator, file, zos); } else { try (FileInputStream fis = new FileInputStream(file); BufferedInputStream bis = new BufferedInputStream(fis)) { zos.putNextEntry(new ZipEntry(path)); int length; while ((length = bis.read(buffer)) > 0) { zos.write(buffer, 0, length); } zos.closeEntry(); } } } } /** * 解压ZIP文件 * * @param zipFile ZIP文件 * @param targetDir 目标目录 * @return 是否成功 */ public static boolean extractZip(File zipFile, File targetDir) { return extractZipWithProgress(zipFile, targetDir, null); } /** * 递归删除文件/目录 * * @param file 要删除的文件或目录 * @return 是否成功 */ public static boolean deleteRecursive(File file) { boolean success = true; if (file.isDirectory()) { File[] children = file.listFiles(); if (children != null) { for (File child : children) { success &= deleteRecursive(child); } } } if (!file.delete()) { Log.w(TAG, "删除失败: " + file.getAbsolutePath()); success = false; } return success; } /** * 删除目录内容(保留目录本身) * * @param directory 要清空的目录 * @return 是否成功 */ public static boolean deleteDirectoryContents(File directory) { if (directory == null || !directory.exists() || !directory.isDirectory()) { return false; } File[] files = directory.listFiles(); if (files == null) return true; // 空目录 boolean success = true; for (File file : files) { if (file.isDirectory()) { success &= deleteRecursive(file); } else { if (!file.delete()) { Log.w(TAG, "删除文件失败: " + file.getAbsolutePath()); success = false; } } } return success; } }
08-20
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值