我尝试使用 System.IO 在 .rar 存档中添加 txt 文件,但每次存档都是空的 - 0kb。
我收到这样的错误消息:
Unhandled Exception: System.IO.IOException: The process cannot access the file 'C:\M\Telegrami\Yambol.zip' because it is being used by another process.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
at System.IO.Compression.ZipFileExtensions.DoCreateEntryFromFile(ZipArchive destination, String sourceFileName, String entryName, Nullable`1 compressionLevel)
at System.IO.Compression.ZipFile.DoCreateFromDirectory(String sourceDirectoryName, String destinationArchiveFileName, Nullable`1 compressionLevel, Boolean includeBaseDirectory, Encoding entryNameEncoding)
at System.IO.Compression.ZipFile.CreateFromDirectory(String sourceDirectoryName, String destinationArchiveFileName, CompressionLevel compressionLevel, Boolean includeBaseDirectory)
at SEND_Plovdiv.WebRequestGetExample.Main() in C:\Users\admin\Desktop\ALL PROJECTS\SEND Plovdiv\SEND Plovdiv\SEND Plovdiv\Program.cs:line 36
有时当我第二次启动项目时,txt 文件被添加到 archive.rar 但错误消息仍然存在。
我想将此txt文件添加到.rar并通过ftp发送..
我的代码在这里:
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace SEND_Plovdiv
{
public class WebRequestGetExample
{
public static void Main()
{
string[] lineOfContents = File.ReadAllLines(@"C:\\M\send.txt");
string username = "";
string password = "";
foreach (var line in lineOfContents)
{
string[] tokens = line.Split(',');
string user = tokens[0];
string pass = tokens[1];
username = user;
password = pass;
}
string pathFile = @"C:\M\Telegrami\";
string zipPath = @"C:\M\Telegrami\Yambol.zip";
ZipFile.CreateFromDirectory(pathFile, zipPath, CompressionLevel.Fastest, true);
// Get the object used to communicate with the server.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://IP to FTP/Yambol.txt");
request.Method = WebRequestMethods.Ftp.UploadFile;
// This example assumes the FTP site uses anonymous logon.
request.Credentials = new NetworkCredential(username, password);
// Copy the contents of the file to the request stream.
StreamReader sourceStream = new StreamReader(@"C:\M\Telegrami\Yambol.txt");
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
request.ContentLength = fileContents.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);
response.Close();
}
}
}
所以存档已创建,但我只想获取文件,因为当我提取时,我只想提取文件而不是文件夹。现在我遇到了新错误,但存档已创建。如果可能的话,我只想获取不带文件夹的 .txt 文件?以及如何消除错误:)
正如评论中提到的,问题是
ZipFile.CreateFromDirectory()
将目录名作为第一个参数,但您传递的是文件名。这是文档:
源目录名称 --- 要归档的目录的路径,指定为相对或 绝对路径。相对路径被解释为相对于 当前工作目录。
这是解决方案:
string pathFile = @"C:\M\Telegrami\";
string zipPath = @"C:\M\Yambol.zip";
ZipFile.CreateFromDirectory(pathFile, zipPath, CompressionLevel.Fastest, true);
如果您需要仅文件,而不是整个目录重写代码,请将方法的最后一个参数
CreateFromDirectory()
从true更改为false:
ZipFile.CreateFromDirectory(pathFile, zipPath, CompressionLevel.Fastest, false);
查看文档:
includeBaseDirectory --- true 包含来自 sourceDirectoryName 的目录名称 存档的根目录; false 仅包含 目录。
您收到错误:
该进程无法访问文件“C:\M\Telegrami\Yambol.zip”,因为 它正在被另一个进程使用。
因为它会尝试将 Yambol.zip 放入当前进程正在使用的文件夹中,因为您同时正在压缩该文件夹。要使其工作,您必须将其放入另一个文件夹中,尝试定义如上所示的文件夹。
来自 LocEngineer 的宝贵意见是使用
.zip
扩展,而不是 .rar
。