fix: 文件操作函数检查文件是否存在

This commit is contained in:
TsMask
2024-07-12 17:42:54 +08:00
parent d0767105b3
commit 04b3fb3e43
3 changed files with 15 additions and 40 deletions

View File

@@ -16,13 +16,9 @@ import (
// data = append(data, []string{"1", "2", "3"})
// err := file.WriterCSVFile(data, filePath)
func WriterFileCSV(data [][]string, filePath string) error {
// 获取文件所在的目录路径
dirPath := filepath.Dir(filePath)
// 确保文件夹路径存在
err := os.MkdirAll(dirPath, 0775)
if err != nil {
logger.Errorf("MkdirAll dir %v", err)
// 创建本地输出目录
if err := os.MkdirAll(filepath.Dir(filePath), 0775); err != nil {
return err
}
// 创建或打开文件

View File

@@ -59,6 +59,11 @@ func CompressZipByFile(zipFilePath, filePath string) error {
// CompressZipByDir 将目录下文件添加到 ZIP 压缩文件
func CompressZipByDir(zipFilePath, dirPath string) error {
// 创建本地输出目录
if err := os.MkdirAll(filepath.Dir(zipFilePath), 0775); err != nil {
return err
}
// 创建输出文件
zipWriter, err := os.Create(zipFilePath)
if err != nil {

View File

@@ -40,8 +40,8 @@ func (s *SSHClientSFTP) CopyDirRemoteToLocal(remoteDir, localDir string) error {
// 遍历远程文件和子目录并复制到本地
for _, remoteFile := range remoteFiles {
remotePath := filepath.Join(remoteDir, remoteFile.Name())
localPath := filepath.Join(localDir, remoteFile.Name())
remotePath := filepath.ToSlash(filepath.Join(remoteDir, remoteFile.Name()))
localPath := filepath.ToSlash(filepath.Join(localDir, remoteFile.Name()))
if remoteFile.IsDir() {
// 如果是子目录,则递归复制子目录
@@ -52,25 +52,10 @@ func (s *SSHClientSFTP) CopyDirRemoteToLocal(remoteDir, localDir string) error {
}
} else {
// 如果是文件,则复制文件内容
remoteFile, err := s.Client.Open(remotePath)
if err != nil {
if err := s.CopyFileRemoteToLocal(remotePath, localPath); err != nil {
logger.Errorf("CopyDirRemoteToLocal failed to opening remote file %s: => %s", remotePath, err.Error())
continue
}
defer remoteFile.Close()
localFile, err := os.Create(localPath)
if err != nil {
logger.Errorf("CopyDirRemoteToLocal failed to creating local file %s: => %s", localPath, err.Error())
continue
}
defer localFile.Close()
_, err = io.Copy(localFile, remoteFile)
if err != nil {
logger.Errorf("CopyDirRemoteToLocal failed to copying file contents from %s to %s: => %s", remotePath, localPath, err.Error())
continue
}
}
}
return nil
@@ -135,6 +120,10 @@ func (s *SSHClientSFTP) CopyDirLocalToRemote(localDir, remoteDir string) error {
// CopyDirRemoteToLocal 复制文件-远程到本地
func (s *SSHClientSFTP) CopyFileRemoteToLocal(remotePath, localPath string) error {
if err := os.MkdirAll(filepath.Dir(localPath), 0775); err != nil {
return err
}
// 打开远程文件
remoteFile, err := s.Client.Open(remotePath)
if err != nil {
@@ -143,21 +132,6 @@ func (s *SSHClientSFTP) CopyFileRemoteToLocal(remotePath, localPath string) erro
}
defer remoteFile.Close()
if err := os.MkdirAll(filepath.Dir(localPath), 0775); err != nil {
return err
}
// 如果目标文件已经存在,先将目标文件重命名
// if info, err := os.Stat(localPath); err == nil && !info.IsDir() {
// ext := filepath.Ext(localPath)
// name := localPath[0 : len(localPath)-len(ext)]
// newName := fmt.Sprintf("%s-%s%s", name, time.Now().Format("20060102_150405"), ext)
// err := os.Rename(localPath, newName)
// if err != nil {
// return err
// }
// }
// 创建本地文件
localFile, err := os.Create(localPath)
if err != nil {