首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >免费开源的.NET多类型文件解压缩组件SharpZipLib(.NET组件介绍之七)

免费开源的.NET多类型文件解压缩组件SharpZipLib(.NET组件介绍之七)

作者头像
彭泽0902
发布于 2018-01-04 08:12:21
发布于 2018-01-04 08:12:21
2.2K00
代码可运行
举报
文章被收录于专栏:C#C#
运行总次数:0
代码可运行

   前面介绍了六种.NET组件,其中有一种组件是写文件的压缩和解压,现在介绍另一种文件的解压缩组件SharpZipLib。在这个组件介绍系列中,只为简单的介绍组件的背景和简单的应用,读者在阅读时可以结合官网的相关介绍和在本地实际操作。

   相关的组件功能非常强大,在笔者的介绍中只是提及到简单的应用,需要了解更多的操作和特性,可以根据官网介绍,或者查看DLL文件的相关类和方法,以此来扩展相关的业务需要。

   SharpZipLib是一个完全在C#中为.NET平台编写的Zip,GZip,Tar和BZip2库。

一.SharpZipLib组件概述:

    ziplib(SharpZipLib,以前的NZipLib)是一个完全在C#为.NET平台编写的Zip,GZip,Tar和BZip2库。它实现为一个程序集(可安装在GAC中),因此可以轻松地集成到其他项目(任何.NET语言)中。 #ziplib的创建者这样说:“我已经将zip库移植到C#,因为我需要gzip / zip压缩,我不想使用libzip.dll或类似的东西我想要的所有在纯C#“。

    SharpZipLib官网提供的下载操作:.NET 1.1,.NET 2.0(3.5,4.0),.NET CF 1.0,.NET CF 2.0的装配:下载237 KB,源代码和示例下载708 KB;源代码和示例下载708 KB;帮助文件下载1208 KB;

    SharpZipLib是在GPL下发布,遵守开源协议。

二.SharpZipLib核心类和方法介绍:

    以上简单的介绍了SharpZipLib组件的相关背景,现在具体看一下该组件的相关核心类和方法:

   1.ZipOutputStream类PutNextEntry():
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
public void PutNextEntry(ZipEntry entry)
{
    bool hasCrc;
    if (entry == null)
    {
        throw new ArgumentNullException("entry");
    }
    if (this.entries == null)
    {
        throw new InvalidOperationException("ZipOutputStream was finished");
    }
    if (this.curEntry != null)
    {
        this.CloseEntry();
    }
    if (this.entries.Count == 0x7fffffff)
    {
        throw new ZipException("Too many entries for Zip file");
    }
    CompressionMethod compressionMethod = entry.CompressionMethod;
    int defaultCompressionLevel = this.defaultCompressionLevel;
    entry.Flags &= 0x800;
    this.patchEntryHeader = false;
    if (entry.Size == 0L)
    {
        entry.CompressedSize = entry.Size;
        entry.Crc = 0L;
        compressionMethod = CompressionMethod.Stored;
        hasCrc = true;
    }
    else
    {
        hasCrc = (entry.Size >= 0L) && entry.HasCrc;
        if (compressionMethod == CompressionMethod.Stored)
        {
            if (!hasCrc)
            {
                if (!base.CanPatchEntries)
                {
                    compressionMethod = CompressionMethod.Deflated;
                    defaultCompressionLevel = 0;
                }
            }
            else
            {
                entry.CompressedSize = entry.Size;
                hasCrc = entry.HasCrc;
            }
        }
    }
    if (!hasCrc)
    {
        if (!base.CanPatchEntries)
        {
            entry.Flags |= 8;
        }
        else
        {
            this.patchEntryHeader = true;
        }
    }
    if (base.Password != null)
    {
        entry.IsCrypted = true;
        if (entry.Crc < 0L)
        {
            entry.Flags |= 8;
        }
    }
    entry.Offset = this.offset;
    entry.CompressionMethod = compressionMethod;
    this.curMethod = compressionMethod;
    this.sizePatchPos = -1L;
    if ((this.useZip64_ == UseZip64.On) || ((entry.Size < 0L) && (this.useZip64_ == UseZip64.Dynamic)))
    {
        entry.ForceZip64();
    }
    this.WriteLeInt(0x4034b50);
    this.WriteLeShort(entry.Version);
    this.WriteLeShort(entry.Flags);
    this.WriteLeShort((byte) entry.CompressionMethodForHeader);
    this.WriteLeInt((int) entry.DosTime);
    if (hasCrc)
    {
        this.WriteLeInt((int) entry.Crc);
        if (entry.LocalHeaderRequiresZip64)
        {
            this.WriteLeInt(-1);
            this.WriteLeInt(-1);
        }
        else
        {
            this.WriteLeInt(entry.IsCrypted ? (((int) entry.CompressedSize) + 12) : ((int) entry.CompressedSize));
            this.WriteLeInt((int) entry.Size);
        }
    }
    else
    {
        if (this.patchEntryHeader)
        {
            this.crcPatchPos = base.baseOutputStream_.Position;
        }
        this.WriteLeInt(0);
        if (this.patchEntryHeader)
        {
            this.sizePatchPos = base.baseOutputStream_.Position;
        }
        if (entry.LocalHeaderRequiresZip64 || this.patchEntryHeader)
        {
            this.WriteLeInt(-1);
            this.WriteLeInt(-1);
        }
        else
        {
            this.WriteLeInt(0);
            this.WriteLeInt(0);
        }
    }
    byte[] buffer = ZipConstants.ConvertToArray(entry.Flags, entry.Name);
    if (buffer.Length > 0xffff)
    {
        throw new ZipException("Entry name too long.");
    }
    ZipExtraData extraData = new ZipExtraData(entry.ExtraData);
    if (entry.LocalHeaderRequiresZip64)
    {
        extraData.StartNewEntry();
        if (hasCrc)
        {
            extraData.AddLeLong(entry.Size);
            extraData.AddLeLong(entry.CompressedSize);
        }
        else
        {
            extraData.AddLeLong(-1L);
            extraData.AddLeLong(-1L);
        }
        extraData.AddNewEntry(1);
        if (!extraData.Find(1))
        {
            throw new ZipException("Internal error cant find extra data");
        }
        if (this.patchEntryHeader)
        {
            this.sizePatchPos = extraData.CurrentReadIndex;
        }
    }
    else
    {
        extraData.Delete(1);
    }
    if (entry.AESKeySize > 0)
    {
        AddExtraDataAES(entry, extraData);
    }
    byte[] entryData = extraData.GetEntryData();
    this.WriteLeShort(buffer.Length);
    this.WriteLeShort(entryData.Length);
    if (buffer.Length > 0)
    {
        base.baseOutputStream_.Write(buffer, 0, buffer.Length);
    }
    if (entry.LocalHeaderRequiresZip64 && this.patchEntryHeader)
    {
        this.sizePatchPos += base.baseOutputStream_.Position;
    }
    if (entryData.Length > 0)
    {
        base.baseOutputStream_.Write(entryData, 0, entryData.Length);
    }
    this.offset += (30 + buffer.Length) + entryData.Length;
    if (entry.AESKeySize > 0)
    {
        this.offset += entry.AESOverheadSize;
    }
    this.curEntry = entry;
    this.crc.Reset();
    if (compressionMethod == CompressionMethod.Deflated)
    {
        base.deflater_.Reset();
        base.deflater_.SetLevel(defaultCompressionLevel);
    }
    this.size = 0L;
    if (entry.IsCrypted)
    {
        if (entry.AESKeySize > 0)
        {
            this.WriteAESHeader(entry);
        }
        else if (entry.Crc < 0L)
        {
            this.WriteEncryptionHeader(entry.DosTime << 0x10);
        }
        else
        {
            this.WriteEncryptionHeader(entry.Crc);
        }
    }
}
   2.ZipOutputStream类Finish():
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
public override void Finish()
{
    if (this.entries != null)
    {
        if (this.curEntry != null)
        {
            this.CloseEntry();
        }
        long count = this.entries.Count;
        long sizeEntries = 0L;
        foreach (ZipEntry entry in this.entries)
        {
            this.WriteLeInt(0x2014b50);
            this.WriteLeShort(0x33);
            this.WriteLeShort(entry.Version);
            this.WriteLeShort(entry.Flags);
            this.WriteLeShort((short) entry.CompressionMethodForHeader);
            this.WriteLeInt((int) entry.DosTime);
            this.WriteLeInt((int) entry.Crc);
            if (entry.IsZip64Forced() || (entry.CompressedSize >= 0xffffffffL))
            {
                this.WriteLeInt(-1);
            }
            else
            {
                this.WriteLeInt((int) entry.CompressedSize);
            }
            if (entry.IsZip64Forced() || (entry.Size >= 0xffffffffL))
            {
                this.WriteLeInt(-1);
            }
            else
            {
                this.WriteLeInt((int) entry.Size);
            }
            byte[] buffer = ZipConstants.ConvertToArray(entry.Flags, entry.Name);
            if (buffer.Length > 0xffff)
            {
                throw new ZipException("Name too long.");
            }
            ZipExtraData extraData = new ZipExtraData(entry.ExtraData);
            if (entry.CentralHeaderRequiresZip64)
            {
                extraData.StartNewEntry();
                if (entry.IsZip64Forced() || (entry.Size >= 0xffffffffL))
                {
                    extraData.AddLeLong(entry.Size);
                }
                if (entry.IsZip64Forced() || (entry.CompressedSize >= 0xffffffffL))
                {
                    extraData.AddLeLong(entry.CompressedSize);
                }
                if (entry.Offset >= 0xffffffffL)
                {
                    extraData.AddLeLong(entry.Offset);
                }
                extraData.AddNewEntry(1);
            }
            else
            {
                extraData.Delete(1);
            }
            if (entry.AESKeySize > 0)
            {
                AddExtraDataAES(entry, extraData);
            }
            byte[] entryData = extraData.GetEntryData();
            byte[] buffer3 = (entry.Comment != null) ? ZipConstants.ConvertToArray(entry.Flags, entry.Comment) : new byte[0];
            if (buffer3.Length > 0xffff)
            {
                throw new ZipException("Comment too long.");
            }
            this.WriteLeShort(buffer.Length);
            this.WriteLeShort(entryData.Length);
            this.WriteLeShort(buffer3.Length);
            this.WriteLeShort(0);
            this.WriteLeShort(0);
            if (entry.ExternalFileAttributes != -1)
            {
                this.WriteLeInt(entry.ExternalFileAttributes);
            }
            else if (entry.IsDirectory)
            {
                this.WriteLeInt(0x10);
            }
            else
            {
                this.WriteLeInt(0);
            }
            if (entry.Offset >= 0xffffffffL)
            {
                this.WriteLeInt(-1);
            }
            else
            {
                this.WriteLeInt((int) entry.Offset);
            }
            if (buffer.Length > 0)
            {
                base.baseOutputStream_.Write(buffer, 0, buffer.Length);
            }
            if (entryData.Length > 0)
            {
                base.baseOutputStream_.Write(entryData, 0, entryData.Length);
            }
            if (buffer3.Length > 0)
            {
                base.baseOutputStream_.Write(buffer3, 0, buffer3.Length);
            }
            sizeEntries += ((0x2e + buffer.Length) + entryData.Length) + buffer3.Length;
        }
        using (ZipHelperStream stream = new ZipHelperStream(base.baseOutputStream_))
        {
            stream.WriteEndOfCentralDirectory(count, sizeEntries, this.offset, this.zipComment);
        }
        this.entries = null;
    }
}
    3.ZipEntry类Clone():
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
public object Clone()
{
    ZipEntry entry = (ZipEntry) base.MemberwiseClone();
    if (this.extra != null)
    {
        entry.extra = new byte[this.extra.Length];
        Array.Copy(this.extra, 0, entry.extra, 0, this.extra.Length);
    }
    return entry;
}
4.ZipOutputStream类Write():
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
public override void Write(byte[] buffer, int offset, int count)
{
    if (this.curEntry == null)
    {
        throw new InvalidOperationException("No open entry.");
    }
    if (buffer == null)
    {
        throw new ArgumentNullException("buffer");
    }
    if (offset < 0)
    {
        throw new ArgumentOutOfRangeException("offset", "Cannot be negative");
    }
    if (count < 0)
    {
        throw new ArgumentOutOfRangeException("count", "Cannot be negative");
    }
    if ((buffer.Length - offset) < count)
    {
        throw new ArgumentException("Invalid offset/count combination");
    }
    this.crc.Update(buffer, offset, count);
    this.size += count;
    switch (this.curMethod)
    {
        case CompressionMethod.Stored:
            if (base.Password != null)
            {
                this.CopyAndEncrypt(buffer, offset, count);
            }
            else
            {
                base.baseOutputStream_.Write(buffer, offset, count);
            }
            break;

        case CompressionMethod.Deflated:
            base.Write(buffer, offset, count);
            break;
    }
}

三.SharpZipLib实例:

  1.压缩单个文件:
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
        /// <summary>
        /// 压缩单个文件
        /// </summary>
        /// <param name="fileToZip">要压缩的文件</param>
        /// <param name="zipedFile">压缩后的文件</param>
        /// <param name="compressionLevel">压缩等级</param>
        /// <param name="blockSize">每次写入大小</param>
        public static void ZipFile(string fileToZip, string zipedFile, int compressionLevel, int blockSize)
        {
            if (string.IsNullOrEmpty(fileToZip))
            {
                throw new ArgumentNullException(fileToZip);
            }
            if (string.IsNullOrEmpty(zipedFile))
            {
                throw new ArgumentNullException(zipedFile);
            }
            if (!File.Exists(fileToZip))
            {
                throw new FileNotFoundException("指定要压缩的文件: " + fileToZip + " 不存在!");
            }
            try
            {
                using (var zipFile = File.Create(zipedFile))
                {
                    using (var zipStream = new ZipOutputStream(zipFile))
                    {
                        using (var streamToZip = new FileStream(fileToZip, FileMode.Open, FileAccess.Read))
                        {
                            var fileName = fileToZip.Substring(fileToZip.LastIndexOf("\\", StringComparison.Ordinal) + 1);
                            var zipEntry = new ZipEntry(fileName);
                            zipStream.PutNextEntry(zipEntry);
                            zipStream.SetLevel(compressionLevel);
                            var buffer = new byte[blockSize];
                            try
                            {
                                int sizeRead;
                                do
                                {
                                    sizeRead = streamToZip.Read(buffer, 0, buffer.Length);
                                    zipStream.Write(buffer, 0, sizeRead);
                                }
                                while (sizeRead > 0);
                            }
                            catch (Exception ex)
                            {
                                throw new Exception(ex.Message);
                            }
                            streamToZip.Close();
                        }
                        zipStream.Finish();
                        zipStream.Close();
                    }
                    zipFile.Close();
                }
            }
            catch (IOException ioex)
            {
                throw new IOException(ioex.Message);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

        }
   2. 压缩单个文件:
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
       /// <summary>
        /// 压缩单个文件
        /// </summary>
        /// <param name="fileToZip">要进行压缩的文件名</param>
        /// <param name="zipedFile">压缩后生成的压缩文件名</param>
        public static void ZipFile(string fileToZip, string zipedFile)
        {
            if (string.IsNullOrEmpty(fileToZip))
            {
                throw new ArgumentException(fileToZip);
            }
            if (string.IsNullOrEmpty(zipedFile))
            {
                throw new ArgumentException(zipedFile);
            }
            if (!File.Exists(fileToZip))
            {
                throw new FileNotFoundException("指定要压缩的文件: " + fileToZip + " 不存在!");
            }
            try
            {
                using (var fs = File.OpenRead(fileToZip))
                {
                    var buffer = new byte[fs.Length];
                    fs.Read(buffer, 0, buffer.Length);
                    fs.Close();
                    using (var zipFile = File.Create(zipedFile))
                    {
                        using (var zipStream = new ZipOutputStream(zipFile))
                        {
                            var fileName = fileToZip.Substring(fileToZip.LastIndexOf("\\", StringComparison.Ordinal) + 1);
                            var zipEntry = new ZipEntry(fileName);
                            zipStream.PutNextEntry(zipEntry);
                            zipStream.SetLevel(5);
                            zipStream.Write(buffer, 0, buffer.Length);
                            zipStream.Finish();
                            zipStream.Close();
                        }
                    }
                }
            }
            catch (IOException ioex)
            {
                throw new IOException(ioex.Message);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

        }
   3.压缩多层目录:
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
        /// <summary>
        /// 压缩多层目录
        /// </summary>
        /// <param name="strDirectory">目录</param>
        /// <param name="zipedFile">压缩文件</param>
        public static void ZipFileDirectory(string strDirectory, string zipedFile)
        {
            if (string.IsNullOrEmpty(strDirectory))
            {
                throw new ArgumentException(strDirectory);
            }
            if (string.IsNullOrEmpty(zipedFile))
            {
                throw new ArgumentException(zipedFile);
            }
            using (var zipFile = File.Create(zipedFile))
            {
                using (var s = new ZipOutputStream(zipFile))
                {
                    ZipSetp(strDirectory, s, "");
                }
            }
        }
    4.递归遍历目录:
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
       /// <summary>
        /// 递归遍历目录
        /// </summary>
        /// <param name="strDirectory">目录</param>
        /// <param name="s">ZipOutputStream对象</param>
        /// <param name="parentPath">父路径</param>
        private static void ZipSetp(string strDirectory, ZipOutputStream s, string parentPath)
        {
            if (strDirectory[strDirectory.Length - 1] != Path.DirectorySeparatorChar)
            {
                strDirectory += Path.DirectorySeparatorChar;
            }
            var crc = new Crc32();

            var filenames = Directory.GetFileSystemEntries(strDirectory);
            try
            {
                // 遍历所有的文件和目录
                foreach (var file in filenames)
                {
                    // 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件
                    if (Directory.Exists(file))
                    {
                        var pPath = parentPath;
                        pPath += file.Substring(file.LastIndexOf("\\", StringComparison.Ordinal) + 1);
                        pPath += "\\";
                        ZipSetp(file, s, pPath);
                    }
                    // 否则直接压缩文件
                    else
                    {
                        //打开压缩文件
                        using (var fs = File.OpenRead(file))
                        {
                            var buffer = new byte[fs.Length];
                            fs.Read(buffer, 0, buffer.Length);
                            var fileName = parentPath + file.Substring(file.LastIndexOf("\\", StringComparison.Ordinal) + 1);
                            var entry = new ZipEntry(fileName)
                            {
                                DateTime = DateTime.Now,
                                Size = fs.Length
                            };
                            fs.Close();
                            crc.Reset();
                            crc.Update(buffer);
                            entry.Crc = crc.Value;
                            s.PutNextEntry(entry);
                            s.Write(buffer, 0, buffer.Length);
                        }
                    }
                }
            }
            catch (IOException ioex)
            {
                throw new IOException(ioex.Message);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

        }
    5.解压缩一个 zip 文件:
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
       /// <summary>
        /// 解压缩一个 zip 文件。
        /// </summary>
        /// <param name="zipedFile">The ziped file.</param>
        /// <param name="strDirectory">The STR directory.</param>
        /// <param name="password">zip 文件的密码。</param>
        /// <param name="overWrite">是否覆盖已存在的文件。</param>
        public void UnZip(string zipedFile, string strDirectory, string password, bool overWrite)
        {
            if (string.IsNullOrEmpty(zipedFile))
            {
                throw new ArgumentException(zipedFile);
            }
            if (string.IsNullOrEmpty(strDirectory))
            {
                throw new ArgumentException(strDirectory);
            }
            if (string.IsNullOrEmpty(password))
            {
                throw new ArgumentException(password);
            }
            if (strDirectory == "")
            {
                strDirectory = Directory.GetCurrentDirectory();
            }
            if (!strDirectory.EndsWith("\\"))
            {
                strDirectory = strDirectory + "\\";
            }
            try
            {
                using (var s = new ZipInputStream(File.OpenRead(zipedFile)))
                {
                    s.Password = password;
                    ZipEntry theEntry;
                    while ((theEntry = s.GetNextEntry()) != null)
                    {
                        var directoryName = string.Empty;
                        var pathToZip = theEntry.Name;
                        if (pathToZip != "")
                        {
                            directoryName = Path.GetDirectoryName(pathToZip) + "\\";
                        }
                        var fileName = Path.GetFileName(pathToZip);
                        Directory.CreateDirectory(strDirectory + directoryName);
                        if (fileName == "") continue;
                        if ((!File.Exists(strDirectory + directoryName + fileName) || !overWrite) &&
                            (File.Exists(strDirectory + directoryName + fileName))) continue;
                        using (var streamWriter = File.Create(strDirectory + directoryName + fileName))
                        {
                            var data = new byte[2048];
                            while (true)
                            {
                                var size = s.Read(data, 0, data.Length);

                                if (size > 0)
                                    streamWriter.Write(data, 0, size);
                                else
                                    break;
                            }
                            streamWriter.Close();
                        }
                    }

                    s.Close();
                }
            }
            catch (IOException ioex)
            {
                throw new IOException(ioex.Message);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

        }

四.总结:

   以上是对SharpZipLib组件的相关介绍,本文的讲解上比较的浅显,如果需要深入的学习可以进入官网进行详细的学习。组件的功能是很强大的,如何在项目中使用组件,完成我们在项目中需要实现的功能,这就是对每个开发者提出了要求,需要我们仔细的去考虑。

   任何学习都需要我们自己去探索和思考,对于一个开发者来说,最重要的就是思考,因为在我们的职业生涯中,没有什么的重要性能够超过思考。如果有不足之处还望各位读者包含,并留言指正。

.NET组件介绍系列:

  一款开源免费的.NET文档操作组件DocX(.NET组件介绍之一)

高效而稳定的企业级.NET Office 组件Spire(.NET组件介绍之二)

 最好的.NET开源免费ZIP库DotNetZip(.NET组件介绍之三)

免费开源的DotNet二维码操作组件ThoughtWorks.QRCode(.NET组件介绍之四)

免费开源的DotNet任务调度组件Quartz.NET(.NET组件介绍之五)

免费高效实用的Excel操作组件NPOI(.NET组件介绍之六)

   免费开源的.NET多类型文件解压缩组件SharpZipLib(.NET组件介绍之七)

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016-12-13 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
Python3 | 练气期,操作运算符,优先级顺序!
描述:上篇介绍了Python3编程基础数据类型,本篇将介绍Python3编程中的有那些运算符以及操作运算符优先级顺序,这也是在Python3编程中非常重要的一个部分,下来就跟随作者快速过一遍,加深一点印象吧!
全栈工程师修炼指南
2024/07/29
2450
Python3 | 练气期,操作运算符,优先级顺序!
python运算符优先级_excel运算符优先级最高的
【例】(1) 求~4, 我们用二进制来表示4: 4 的原码: 0000 0100 取反得到: 1111 1011, 观察符号,是负数,因为负数以补码存储的,所以问题转化为: 某个数 x 的补码是 1111 1011,求 x 的值(由补码求原码) 取反: 0000 0100 +1: 0000 0101 = 5, 加上标点符号(负号) 得到结果: -5
全栈程序员站长
2022/11/18
8160
python 函数、运算符以及运算符优先级
Python函数的基本介绍 什么是函数? 函数是一段可以直接被另外一段程序或代码引用的程序或代码, 也叫做子程序, 方法. 可重复使用 可互相调用 函数的目的 为了代码段的复用 在Python中如何定
友儿
2022/09/26
4830
python变量、运算符
变量 变量就是可以重复使用的一个量,或者叫一个代号 变量命名的规则 - 变量命名可以包含数字,大小写字母,下划线或者更多,但是我们不推荐除了前三种内容之外的符号 - 数字不可以打头 - 4man,5for是不可以的 - man4,for5是可以的 - 一般在python中,以下划线开头的内容具有特殊含义,不建议使用 - 比如\_age, \_name, 理论可以,但强烈不推荐,包括但不限于一个下划线开头和两个连续下划线开头 - 大小写不一样,俗称大小写敏感 - ForMan跟
ruochen
2021/05/10
1.5K0
python变量、运算符
【Python】从基础到进阶(三):深入了解Python中的运算符与表达式
在前两篇文章中,我们已经了解了Python的基础知识,包括基本的数据类型和数据类型转换,以及基本的输入输出操作。在实际编程过程中,运算符和表达式是不可或缺的部分,它们不仅用于执行基本的算术运算,还用于比较值、进行逻辑判断和操作位等。理解并熟练使用各种运算符和表达式,将大大提高代码的可读性和编写效率。
空白诗
2024/07/20
2680
【Python】从基础到进阶(三):深入了解Python中的运算符与表达式
C++运算符优先级
C++运算符优先级,是描述在计算机运算计算表达式时执行运算的先后顺序。 先执行具有较高优先级的运算,然后执行较低优先级的运算。 例如,我们常说的先执行相乘和除,再执行加减运算。
老九学堂-小师弟
2019/10/09
4.8K0
C运算符及优先级
1、常用运算符分类 运算符类型 作用 算术运算符 用于处理四则运算 赋值运算符 用于将表达式的值赋给变量 比较运算符 用于表达式的比较,并返回一个真值或假值 逻辑运算符 用于根据表达式的值返回真值或假值 位运算符 用于处理数据的位运算 sizeof运算符 用于求字节数长度 2、算术运算符 运算符 术语 示例 结果 + 正号 +3 3
mcxfate
2020/08/02
5240
Cpp运算符优先级
欢迎与我分享你的看法。 转载请注明出处:http://taowusheng.cn/
yifei_
2022/11/14
6410
Python学习笔记(3):运算符与表达式[通俗易懂]
下表运算符是从最低到最高。Python会首先计算表中最下面的运算符,然后再计算表上部的。实际上强烈建议使用圆括号来分组运算符和操作数,以便明确地指出运算的先后顺序,达到易读。
全栈程序员站长
2022/09/07
4120
Python全网最全基础课程笔记(三)——所有运算符+运算符优先级
Python中的运算符优先级决定了在包含多个运算符的表达式中,各个运算符的执行顺序。优先级高的运算符会先于优先级低的运算符执行。以下是Python中所有运算符的优先级列表,按照从高到低的顺序排列。
小白的大数据之旅
2024/11/20
5090
Python全网最全基础课程笔记(三)——所有运算符+运算符优先级
Java运算符及运算符的优先级
赋值运算符算术运算符关系运算符逻辑运算符位运算符三目运算符instanceof运算符
用户7886150
2021/04/25
1.2K0
Python基础之注释,算数运算符,变量
注释的作用:用自己熟悉的语言,对某些代码进行标注说明,增强程序的可读性; 在python解释器解释代码的过程中,凡是#右边的,解释器都直接跳过这一行;
py3study
2020/01/17
7180
Python运算符优先级
#!/usr/bin/python # -*- coding: UTF-8 -*- a = 20 b = 10 c = 15 d = 5 e = 0 e = (a + b) * c / d #( 30 * 15 ) / 5 print "(a + b) * c / d 运算结果为:", e e = ((a + b) * c) / d # (30 * 15 ) / 5 print "((a + b) * c) / d 运算结果为:", e e = (a + b) * (c / d); # (30) * (15/5) print "(a + b) * (c / d) 运算结果为:", e e = a + (b * c) / d; # 20 + (150/5) print "a + (b * c) / d 运算结果为:", e
用户8442333
2021/05/26
6510
《简明 Python 教程》学习笔记-运算符与表达式
又两天没更新了,暂且同步两篇之前的笔记。运算符与表达式这一块感觉主要就是运算符与它们的用法以及优先级了。
WindCoder
2018/09/20
4430
C语言运算符详解
C语言包含多种运算符,如算术、关系、逻辑、位、赋值和条件等,它们用于执行各种计算和操作,如加减乘除、比较、逻辑判断、位运算、赋值和条件控制等,是C语言编程中不可或缺的元素。
鲜于言悠
2024/05/24
1710
Python基础之运算符
1 算术运算符2 位运算符3 比较运算符4 赋值运算符5 身份运算符6 成员运算符7 逻辑运算符
用户7886150
2020/11/26
6840
C语言运算符优先级列表(超详细)
每当想找哪个运算符优先级高时,很多时候总是想找的就没有,真让人气愤!现在,终于有个我个人觉得非常全的,分享给大家。
全栈程序员站长
2022/11/09
10.6K0
Python 运算符
文字操作系统与外部最主要的接口就叫做 Shell。Shell 是操作系统最外面的一层。Shell 管理你与操作系统之间的交互:等待你输入,向操作系统解释你的输入,并且处理各种各样的操作系统的输出结果。
忆想不到的晖
2021/12/06
7040
Python 运算符
【C语言】C语言运算符优先级详解
在C语言中,运算符的优先级决定了表达式中各个运算符的计算顺序。了解这些优先级对于正确理解和编写复杂表达式至关重要。本文将深入探讨C语言中各种运算符的优先级及其影响。
学习起来吧
2024/03/23
1.3K0
【C语言】C语言运算符优先级详解
深入了解Python运算符和表达式:从基础到高级
Python运算符和表达式是编程中的核心概念,用于执行各种计算和操作。在本文中,我们将深入介绍Python运算符和表达式的各个方面,包括算术运算符、比较运算符、逻辑运算符、位运算符等,以帮助你更好地理解和应用它们。
海拥
2023/09/19
8430
深入了解Python运算符和表达式:从基础到高级
推荐阅读
相关推荐
Python3 | 练气期,操作运算符,优先级顺序!
更多 >
交个朋友
加入[腾讯云] DeepSeek开发者交流群
前沿技术深度讨论 发展开发者人脉圈
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档