Loading [MathJax]/jax/input/TeX/config.js
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
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 删除。

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

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
NodeJS文件系统(fs)与流(stream)
文件标记(flags): http://nodejs.cn/api/fs.html#fs_fs_open_path_flags_mode_callback
九旬
2020/10/23
1.5K0
NodeJS 读写文件 🎠
操作文件主要包括读和写。而这些功能 NodeJS 都已经提供了对应的方法。只要调用就行了。
德育处主任
2022/08/30
2.1K0
Node.js 中使用 fs 模块进行文件读写操作详解
在现代 Web 开发中,Node.js 以其独特的非阻塞 I/O 和事件驱动架构,已经成为服务器端开发的首选平台之一。而在 Node.js 的核心模块中,fs(文件系统)模块扮演着至关重要的角色。它提供了丰富的 API,使得开发者能够轻松地进行文件的读取、写入、追加、复制和删除等操作。本文将深入探讨 fs 模块的使用方法,通过详细的示例代码,帮助读者掌握在 Node.js 环境下进行文件操作的基本技能。
Front_Yue
2024/12/25
6950
Node.js 中使用 fs 模块进行文件读写操作详解
nodeJs基础Api
所有能够触发事件的对象都是EventEmitter类的实例,这个对象开放了EventEmitter.on()函数允许将一个或多个函数绑定到被对象触发的命名事件上。
切图仔
2022/09/14
3640
node 相关知识学习
一:file模块 1、两种导出方式 module.exports = {fn,student} 或 exports.fn = fn 2、同步把内容写入到文件 let fs = require('fs') // 同步打开文件 let fd = fs.openSync('text.txt','w') // 写入内容 let str = '臭鱼111' fs.writeFileSync(fd,str) // 退出文件 fs.close
xyzzz
2021/08/01
2500
nodejs操作文件系统(一)
Node.js 提供一组类似 UNIX(POSIX)标准的文件操作API。 Node 导入文件系统模块(fs)语法如下所示:
OECOM
2020/07/01
1.1K0
【快速复习】Node.js中的fs模块的使用
JavaScript 的是没有操作文件的能力,但是 Node 是可以做到的,Node 提供了操作文件系统模块,是 Node 中使用非常重要和高频的模块,是绝对要掌握的一个模块系统。
张张
2020/03/06
1.5K0
【快速复习】Node.js中的fs模块的使用
Node.js学习笔记(二)——Node.js模块化、文件读写、环境变量
(1)、在浏览器端使用var或不使用关键字定义的变量属于全局作用域,也就是可以使用window对象访问。
张果
2022/09/28
6.5K0
Node.js学习笔记(二)——Node.js模块化、文件读写、环境变量
邂逅Node.JS的那一夜
本篇文章,并不完全适合小白,需要有一定的HTML、CSS、JS、HTTP、Web等知识及基础学习:
Java_慈祥
2024/08/03
2410
邂逅Node.JS的那一夜
Node入门教程(9)第七章:NodeJs的文件处理
Node的文件处理涉及到前面说的ptah模块,以及fs文件系统、stream流处理、Buffer缓冲器等模块。内容可能比较多,相关内容请以官网文档为主,此处主要以案例讲解为主,分享给大家一些常用的经典案例。细节就不展开了。 fs文件系统 fs模块提供了很多文件操作相关的api,比如:监控文件夹、文件,文件重命名,文件读写,文件修改权限、文件读写流等。 在此,我们仅以几个案例的方式来驱动学习Node的文件系统,细节请详细阅读Node的api文档或者源码。 案例: 如何监控文件夹的变化? 如何读取一个文
老马
2018/04/16
1.5K0
Node.js 高级进阶之 fs 文件模块学习
文件操作是开发过程中并不可少的一部分,作为一名 Node.js 开发工程师更应该熟练掌握fs模块的相关技巧。Node.js 中的 fs 模块是文件操作的封装,它提供了文件读取、写入、更名、删除、遍历目录、链接等 POSIX 文件系统操作。与其它模块不同的是,fs 模块中所有的操作都提供了异步和同步的两个版本,具有 sync 后缀的方法为同步方法,不具有 sync 后缀的方法为异步方法
coder_koala
2019/07/30
1.6K0
Node.js 高级进阶之 fs 文件模块学习
nodejs(一)
使用快捷键(Windows徽标键+ R)打开运行面板,输入cmd 后直接回车,即可打开终端。
且陶陶
2023/04/12
6260
nodejs(一)
【Node.js】01 —— fs模块全解析
另外,Node.js 还提供了诸如 fs.readdir()(异步读取目录内容)和 fs.readdirSync()(同步读取目录内容)等方法,用于枚举指定目录中的文件和子目录。同时,还有 fs.promises.readdir() 提供基于Promise的异步API。
空白诗
2024/06/14
5640
node之http模块之fs模块(续)
爱学习的前端歌谣
2023/12/21
1570
node之http模块之fs模块(续)
05_Node js 文件管理模块 fs
会发现目录下多了一个 index.js 文件夹,并且添加了“hello NodeJS!”的内容。
全栈程序员站长
2022/06/30
1.1K0
Node·七天学会 NodeJS
以上程序使用 fs.readFileSync 从源路径读取文件内容,并使用 fs.writeFileSync 将文件内容写入目标路径。
数媒派
2022/12/01
2.3K0
nodejs中的文件系统
nodejs使用了异步IO来提升服务端的处理效率。而IO中一个非常重要的方面就是文件IO。今天我们会详细介绍一下nodejs中的文件系统和IO操作。
程序那些事
2021/01/28
1.5K0
Nodejs中对文件执行读写操作(多demo)
在nodejs中实现对文件及目录读写操作的功能是fs模块。另外与文件及目录操作相关的一个模块是path模块。
前端_AWhile
2019/08/29
2K0
【Nodejs】240-有助于理解前端工具的 node 知识
缘起 平时写惯了业务代码之后,如果想要了解下 webpack 或者 vue-cli,好像是件很难上手的事情? 。拿 webpack 来说,我们可能会对配置熟悉点,但常常一段时间过后又忘了,感觉看起来不
pingan8787
2019/07/25
4990
【Nodejs】240-有助于理解前端工具的 node 知识
《Node.js》核心技术教程(笔记)
模块化是一种设计思想,利用模块化可以把一个非常复杂的系统结构细化到具体的功能点,每个功能点看作一个模块,然后通过某种规则把这些小的模块组合到一起,构成模块化系统。
爱学习的程序媛
2022/04/07
1.9K0
《Node.js》核心技术教程(笔记)
相关推荐
NodeJS文件系统(fs)与流(stream)
更多 >
LV.0
这个人很懒,什么都没有留下~
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验