无法使用golang删除未压缩的文件夹。

huangapple go评论113阅读模式
英文:

Unable to delete an unzipped folder using golang

问题

我写了一段代码,用于解压缩文件到指定位置,然后将文件夹的内容复制到解压缩的文件夹外部,最后删除文件夹。

问题是除了删除文件夹之外,其他都正常工作。文件夹中只有一个文件。文件的位置如下:

  1. E:\go\copyDirectory\myfile\mytextfile.txt

压缩文件的位置如下:

  1. E:\go\copyDirectory\myfile.zip

压缩文件中只有一个文本文件。压缩文件中的文件如下:

  1. E:\go\copyDirectory\myfile.zip\myfile\mytextfile.txt

我得到的错误是:

  1. ERRR::: remove myfile\mytextfile.txt: The process cannot
  2. access the file because it is being used by another process.

谢谢。

英文:

I wrote code that unzips a file in a particular location then copies the contents of the folder to outside where the folder is unzipped then it removes the folder.

This is the Code I wrote:

  1. package main
  2. import (
  3. "os"
  4. "flag"
  5. "fmt"
  6. "io"
  7. "path/filepath"
  8. "os/exec"
  9. "archive/zip"
  10. "time"
  11. )
  12. func RemoveContents(dir string) error {
  13. d, err := os.Open(dir)
  14. if err != nil {
  15. return err
  16. }
  17. names, err := d.Readdirnames(-1)
  18. if err != nil {
  19. return err
  20. }
  21. for _, name := range names {
  22. err = os.RemoveAll(filepath.Join(dir, name))
  23. if err != nil {
  24. return err
  25. }
  26. }
  27. d.Close()
  28. return nil
  29. }
  30. func CopyFile(source string, dest string) (err error) {
  31. sourcefile, err := os.Open(source)
  32. if err != nil {
  33. return err
  34. }
  35. defer sourcefile.Close()
  36. destfile, err := os.Create(dest)
  37. if err != nil {
  38. return err
  39. }
  40. defer destfile.Close()
  41. _, err = io.Copy(destfile, sourcefile)
  42. if err == nil {
  43. sourceinfo, err := os.Stat(source)
  44. if err != nil {
  45. err = os.Chmod(dest, sourceinfo.Mode())
  46. }
  47. }
  48. return
  49. }
  50. func CopyDir(source string, dest string) (err error) {
  51. // get properties of source dir
  52. sourceinfo, err := os.Stat(source)
  53. if err != nil {
  54. return err
  55. }
  56. // create dest dir
  57. err = os.MkdirAll(dest, sourceinfo.Mode())
  58. if err != nil {
  59. return err
  60. }
  61. directory, _ := os.Open(source)
  62. objects, err := directory.Readdir(-1)
  63. for _, obj := range objects {
  64. sourcefilepointer := source + "/" + obj.Name()
  65. destinationfilepointer := dest + "/" + obj.Name()
  66. if obj.IsDir() {
  67. // create sub-directories - recursively
  68. err = CopyDir(sourcefilepointer, destinationfilepointer)
  69. if err != nil {
  70. fmt.Println(err)
  71. }
  72. } else {
  73. // perform copy
  74. err = CopyFile(sourcefilepointer, destinationfilepointer)
  75. if err != nil {
  76. fmt.Println(err)
  77. }
  78. }
  79. }
  80. return
  81. }
  82. func main() {
  83. flag.Parse() // get the source and destination directory
  84. source_dir := flag.Arg(0) // get the source directory from 1st argument
  85. dest_dir := flag.Arg(1) // get the destination directory from the 2nd argument
  86. os.MkdirAll("E:\\go\\copyDirectory\\myFile.zip",0777)
  87. zipFilePath := "E:\\go\\copyDirectory\\myFile.zip"
  88. tempWrkDir := "E:\\go\\copyDirectory\\"
  89. //Read zip file and get path handle.
  90. fileHandleReader, err := zip.OpenReader(zipFilePath)
  91. if err != nil {
  92. fmt.Println(err)
  93. os.Exit(1)
  94. }
  95. //open zip file and read all the folder and files inside
  96. for _, fileReadHandler := range fileHandleReader.Reader.File {
  97. //read the file or folder handle inside zip
  98. fileOpenHandle, err := fileReadHandler.Open()
  99. if err != nil {
  100. fmt.Println(err)
  101. os.Exit(1)
  102. }
  103. defer fileOpenHandle.Close()
  104. targetUnZipPath := filepath.Join(tempWrkDir, fileReadHandler.Name)
  105. if fileReadHandler.FileInfo().IsDir() {
  106. os.MkdirAll(targetUnZipPath, fileReadHandler.Mode())
  107. //fmt.Println("Creating directory", path)
  108. }else {
  109. // create new dummy file to copy original file.
  110. newTempFileHandle, err := os.OpenFile(targetUnZipPath, os.O_WRONLY|os.O_CREATE, fileReadHandler.Mode())
  111. if err != nil {
  112. fmt.Println(err)
  113. os.Exit(1)
  114. }
  115. defer newTempFileHandle.Close()
  116. //copying original file to dummy file.
  117. if _, err = io.Copy(newTempFileHandle, fileOpenHandle); err != nil {
  118. fmt.Println(err)
  119. os.Exit(1)
  120. }
  121. }
  122. }
  123. time.Sleep(1000*time.Millisecond)
  124. fmt.Println("Source :" + source_dir)
  125. // check if the source dir exist
  126. src, err := os.Stat(source_dir)
  127. if err != nil {
  128. panic(err)
  129. }
  130. if !src.IsDir() {
  131. fmt.Println("Source is not a directory")
  132. os.Exit(1)
  133. }
  134. // create the destination directory
  135. fmt.Println("Destination :"+ dest_dir)
  136. /*_, err = os.Open(dest_dir)
  137. if !os.IsNotExist(err) {
  138. fmt.Println("Destination directory already exists. Abort!")
  139. os.Exit(1)
  140. }*/
  141. err = CopyDir(source_dir, dest_dir)
  142. if err != nil {
  143. fmt.Println(err)
  144. } else {
  145. fmt.Println("Directory copied")
  146. }
  147. err = RemoveContents("./myFiles")
  148. if err != nil {
  149. fmt.Println("ERRR:::",err)
  150. }
  151. //time.Sleep(10000*time.Millisecond)
  152. }

The problem is that everything works fine except for deleting the folder. The folder has only one file in it. The location of the file is as follows:

  1. E:\go\copyDirectory\myfile\mytextfile.txt

The Location of the zip file is as follows:

  1. E:\go\copyDirectory\myfile.zip

The zip file has only one text file. The File inside the zip file is as follows:

  1. E:\go\copyDirectory\myfile.zip\myfile\mytextfile.txt

The error I get is:

  1. ERRR::: remove myfile\mytextfile.txt: The process cannot
  2. access the file because it is being used by another process.

Thanks in advance.

答案1

得分: 2

你没有关闭文件。这段代码:

  1. defer newTempFileHandle.Close()

在主函数结束后执行,也就是在以下代码之后:

  1. err = RemoveContents("./myFiles")

你可以将这段代码放在一个无名函数中:

  1. func() {
  2. // 在 zip 文件中读取文件或文件夹句柄
  3. fileOpenHandle, err := fileReadHandler.Open()
  4. if err != nil {
  5. fmt.Println(err)
  6. os.Exit(1)
  7. }
  8. defer fileOpenHandle.Close()
  9. targetUnZipPath := filepath.Join(tempWrkDir, fileReadHandler.Name)
  10. if fileReadHandler.FileInfo().IsDir() {
  11. os.MkdirAll(targetUnZipPath, fileReadHandler.Mode())
  12. //fmt.Println("Creating directory", path)
  13. } else {
  14. // 创建一个新的虚拟文件来复制原始文件
  15. newTempFileHandle, err := os.OpenFile(targetUnZipPath, os.O_WRONLY|os.O_CREATE, fileReadHandler.Mode())
  16. if err != nil {
  17. fmt.Println(err)
  18. os.Exit(1)
  19. }
  20. defer newTempFileHandle.Close()
  21. // 将原始文件复制到虚拟文件中
  22. if _, err = io.Copy(newTempFileHandle, fileOpenHandle); err != nil {
  23. fmt.Println(err)
  24. os.Exit(1)
  25. }
  26. }
  27. }()

这样,你的 defer 语句将在尝试删除文件之前执行。不过我建议将这部分代码提取到一个有名字的函数中。

英文:

You aren't closing the file. This:

  1. defer newTempFileHandle.Close()

Is run when main finishes, which is after:

  1. err = RemoveContents("./myFiles")

You can wrap that bit of code in an unnamed function:

  1. func() {
  2. //read the file or folder handle inside zip
  3. fileOpenHandle, err := fileReadHandler.Open()
  4. if err != nil {
  5. fmt.Println(err)
  6. os.Exit(1)
  7. }
  8. defer fileOpenHandle.Close()
  9. targetUnZipPath := filepath.Join(tempWrkDir, fileReadHandler.Name)
  10. if fileReadHandler.FileInfo().IsDir() {
  11. os.MkdirAll(targetUnZipPath, fileReadHandler.Mode())
  12. //fmt.Println("Creating directory", path)
  13. } else {
  14. // create new dummy file to copy original file.
  15. newTempFileHandle, err := os.OpenFile(targetUnZipPath, os.O_WRONLY|os.O_CREATE, fileReadHandler.Mode())
  16. if err != nil {
  17. fmt.Println(err)
  18. os.Exit(1)
  19. }
  20. defer newTempFileHandle.Close()
  21. //copying original file to dummy file.
  22. if _, err = io.Copy(newTempFileHandle, fileOpenHandle); err != nil {
  23. fmt.Println(err)
  24. os.Exit(1)
  25. }
  26. }
  27. }()

And then your defer will happen before you try and remove the files. I would recommend pulling this out into a named function though.

huangapple
  • 本文由 发表于 2016年1月14日 18:51:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/34787709.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定