简体   繁体   English

C#-文件压缩重命名操作或不使用GZipStream进行压缩

[英]C# - Files compressed rename operation or compress without GZipStream

I've a problematic situation where I should compress an file and so rename it to what the user choose + ".zip"/".rar"/".tar.gz"/".tar". 我遇到一个问题,我应该压缩文件,然后将其重命名为用户选择的名称+“ .zip” /“。rar” /“。tar.gz” /“。tar”。

About compress itself is everything Ok, but when I try to rename the file with something like File.Move() or FileInfo.Move() , the name of the compressed file also changes, just like the file extension. 关于compress本身是可以的,但是当我尝试使用File.Move()FileInfo.Move()类的文件重命名文件时,压缩文件的名称也会更改,就像文件扩展名一样。 Example: 例:

string pathFile = "C:\\Users\\Admin\\Desktop\\myFile.exe";
string finalPath = "C:\\Users\\Admin\\Desktop\\userFile.zip";
string compressedPath = "C:\\Users\\Admin\\Desktop\\myFile.exe.zip";

... ...

File.Move(compressedPath, finalPath);

The problem here is who userFile.zip , when decompressed, generates a userFile file, without extension. 问题是谁将userFile.zip解压缩后生成一个没有扩展名的userFile文件。 Previously I read that compressed files by GZIP don't have information bisides the byte[] array who was written, and this is the possible cause. 以前,我读过GZIP压缩文件的信息不包含写入的byte []数组,这是可能的原因。

But I want to know if someone knows a way to rename GZIP files or another way to compress files and rename with .NET framework. 但是我想知道是否有人知道重命名GZIP文件的方法或另一种压缩文件并使用.NET Framework重命名的方法

Thank you. 谢谢。

https://msdn.microsoft.com/en-us/library/ms404280(v=vs.110).aspx https://msdn.microsoft.com/zh-CN/library/ms404280(v=vs.110).aspx

using System;
using System.IO;
using System.IO.Compression;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string startPath = @"c:\example\start";
            string zipPath = @"c:\example\result.zip";
            string extractPath = @"c:\example\extract";

            ZipFile.CreateFromDirectory(startPath, zipPath);

            ZipFile.ExtractToDirectory(zipPath, extractPath);
        }
    }
}

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

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