2.8 KiB
2.8 KiB
跨平台文件大小计算工具
概述
这个工具解决了不同操作系统下文件大小计算的差异问题:
- Windows: 使用 1024 进制 (二进制,1KB = 1024 bytes)
- macOS: 使用 1000 进制 (十进制,1KB = 1000 bytes)
- Linux: 使用 1000 进制 (十进制,遵循 SI 标准,1KB = 1000 bytes)
主要功能
1. 操作系统检测
import { getOS } from '/src/renderer/utils/fileSize'
const currentOS = getOS() // 'windows', 'mac', 'linux', 'unknown'
2. 获取基础单位大小
import { getBaseSize } from '/src/renderer/utils/fileSize'
const baseSize = getBaseSize() // 1024 (Windows/Linux) 或 1000 (macOS)
3. 光盘类型配置
import { getCurrentOSDiscTypes } from '/src/renderer/utils/fileSize'
const discTypes = getCurrentOSDiscTypes()
// 返回当前系统适配的光盘类型配置
4. 文件大小格式化
import { formatFileSize } from '/src/renderer/utils/fileSize'
const formatted = formatFileSize(1024*1024*1024) // "1.00 GB" (Windows) 或 "1.00 GB" (macOS)
使用示例
在组件中使用
// dashboard/index.vue
import { getCurrentOSDiscTypes } from '/src/renderer/utils/fileSize'
export default {
methods: {
updateDiscTypes() {
const typeLists = getCurrentOSDiscTypes()
this.$store.dispatch('chat/setDatas', { name: 'cd_types', data: typeLists })
}
}
}
在工具函数中使用
// utils/index.js
import { formatFileSize } from '/src/renderer/utils/fileSize'
export const filterSize = (size) => {
if (!size || size < 0) return '0 B'
return formatFileSize(size)
}
光盘类型配置
工具自动为不同操作系统生成正确的光盘容量配置:
| 类型 | Windows | macOS/Linux |
|---|---|---|
| CD 700MB | 734,003,200 bytes | 700,000,000 bytes |
| DVD 4.7GB | 5,046,586,572 bytes | 4,700,000,000 bytes |
| BD 25GB | 26,843,545,600 bytes | 25,000,000,000 bytes |
调试功能
import { getDebugInfo } from '/src/renderer/utils/fileSize'
const debug = getDebugInfo()
console.log(debug)
// 输出当前系统的完整配置信息
测试
运行测试文件查看不同系统的差异:
import { testFileSizeCalculation, compareOSDifferences } from '/src/renderer/utils/fileSizeTest'
testFileSizeCalculation() // 显示当前系统配置
compareOSDifferences() // 对比不同系统差异
注意事项
- 工具会自动检测当前操作系统
- 所有大小计算都基于字节 (bytes)
- 格式化显示会根据系统使用相应的进制
- 光盘容量配置会自动适配当前系统
更新历史
- 2024-01-XX: 初始版本,支持 Windows/macOS/Linux 跨平台文件大小计算