简体   繁体   English

该进程无法访问该文件,因为它正被 Golang 中的另一个进程使用

[英]The process cannot access the file because it is being used by another process in Golang

The process cannot access the file ... because it is being used by another process该进程无法访问该文件...因为它正被另一个进程使用

I can't Remover Zip file with this code ..我无法使用此代码移除 Zip 文件..

it's possible?这是可能的? extract and delete the file in one code.在一个代码中提取和删除文件。

Code代码

package main

import (
    "archive/zip"
    "fmt"
    "io"
    "log"
    "net/http"
    "os"
    "path/filepath"
    "strings"
)

func main() {
    url := "https://230c07c8-77b2-4c0d-9b82-8c6501a5bc45.filesusr.com/archives/b7572a_9ec985e0031042ef912cb40cafbe6376.zip?dn=7.zip"
    out, _ := os.Create("E:\\experi\\1234567890.zip")
    defer out.Close()
    resp, _ := http.Get(url)
    defer resp.Body.Close()
    _, _ = io.Copy(out, resp.Body)
    files, err := Unzip("E:\\experi\\1234567890.zip", "E:\\experi\\1234567890")
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println("Unzipped the following files:\n" + strings.Join(files, "\n"))
}

func Unzip(src string, destination string) ([]string, error) {

    var filenames []string

    r, err := zip.OpenReader(src)

    if err != nil {


        return filenames, err
    }

    defer r.Close()

    for _, f := range r.File {

        fpath := filepath.Join(destination, f.Name)

        if !strings.HasPrefix(fpath, filepath.Clean(destination)+string(os.PathSeparator)){
            return filenames, fmt.Errorf("%s is an illegal filepath", fpath)
        }

        filenames = append(filenames, fpath)

        if f.FileInfo().IsDir() {

            os.MkdirAll(fpath, os.ModePerm)
            continue
        }
        if err = os.MkdirAll(filepath.Dir(fpath), os.ModePerm); err != nil {
            return filenames, err
        }
        outFile, err := os.OpenFile(fpath,
            os.O_WRONLY|os.O_CREATE|os.O_TRUNC | os.O_RDWR,
            f.Mode())
        if err != nil {
            return filenames, err
        }

        rc, err := f.Open()

        if err != nil {
            return filenames, err
        }

        _, err = io.Copy(outFile, rc)
        outFile.Close()
        rc.Close()

        if err != nil {
            return filenames, err
        }
    }
    
    removeFile()
    return filenames, nil
}


func removeFile()  {
    error := os.Remove("E:\\experi\\1234567890.zip")
    if error != nil {
        log.Fatal(error)
    }
}

Output输出

output text输出文本

2020/10/28 13:09:04 remove E:\experi\1234567890.zip: The process cannot access the file because it is being used by another process.

Process finished with exit code 1

输出图像

Any other way to do this same thing ?还有其他方法可以做同样的事情吗?

Did I go wrong anywhere?我哪里出错了?

Help Would be Much Appreciated.帮助将不胜感激。 Thanks in Advance.提前致谢。 :) :)

out, _ := os.Create("E:\\\\experi\\\\1234567890.zip") creates or truncates the file and returns you a *File (so the file is open). out, _ := os.Create("E:\\\\experi\\\\1234567890.zip")创建或截断文件并返回一个*File (因此文件已打开)。

defer out.Close() closes the file "the moment the surrounding function returns" ( spec ). defer out.Close()在“周围函数返回的那一刻”关闭文件( spec )。

So at the time you call Unzip you have the file open.因此,在您调用Unzip您已打开文件。 To fix this call out.Close() before the call to Unzip (and please don't assume that calls complete without error).为了解决这个问题调用out.Close()调用之前Unzip (请不要以为来电完整无误差)。

If you close using the defer, it is closed after performing up to the last line of the function.如果使用 defer 关闭,它会在执行到函数的最后一行后关闭。 You must explicitly close the file before remove it.在删除文件之前,您必须明确关闭该文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM