zoukankan      html  css  js  c++  java
  • node解压压缩包以及压缩图片

    node解压压缩包以及压缩图片

    首先保证电脑安装node环境,下载地址:http://nodejs.cn

    //可以打开一个dos窗口输入node -v进行确认是否安装成功
    C: > node -v
        > v10.*
    

      

    然后就是安装解压压缩包的第三方插件 和 tinypng的插件

    npm install compressing //解压压缩包的第三方插件
    npm isntall tinify //解压图片的压缩

    如何使用?

    解压压缩包,这里以解压tgz格式的压缩包为例,compressing的API:https://www.npmjs.com/package/compressing

    var compressing = require("compressing")
    let nowFilePath = "01.tgz" //当前目录下解压的文件名 let positionFilePath = "01"//需要解压到当前目录下的目录名 compressing.tgz.uncompress(nowFilePath, positionFilePath) .then(() => { console.log(nowFilePath + "解压完成") }) .catch(err => { console.log("解压失败:"+err); })

    使用递归的方法去解压多个压缩包

    //定义一个函数
    function decompressionFiles(files,length){
    if(length==0){ 
    //在这里我们确定压缩包全部解压后可以操作的事情
    return console.log("没有找到压缩包,或压缩已完成!")
    }
    let positionFilePath=files.replace(".tgz","") 
    compressing.tgz.uncompress(files[length-1], positionFilePath)
    .then(() => {
    console.log(nowFilePath + "解压完成")
    decompressionFiles(files,length-1)
    })
    .catch(err => {
    console.log("解压失败:"+err);
    })
    }
    
    var fileArray=["01.tgz","02.tgz","03.tgz","04.tgz"] //在这里假设是当前目录下的01.tgz等压缩包
    
    //在这里我们只需调用函数即可
    decompressionFiles(fileArray,fileArray.length)
    

    博主还写了一个windows本地压缩图片的工具,windows可执行文件,点击运行,根据指令批量压缩图片。

    码云下载地址:https://gitee.com/kong_yiji_and_lavmi/imagemin

     压缩图片[tinify的API](https://www.npmjs.com/package/tinify)(熊猫压缩)[申请key](https://tinypng.com/developers)

    var tinify = require("tinify");
    var path= require("path");
    tinify.key = "YOU.RAPI.KEY"; //这里需要填写你在熊猫注册的key,只需要一个邮箱即可
    let nowFilePath = path.join(dirname,"01.jpg") //假设是当前目录下的01.jpg
    let filePositionPath = path.join(dirname,"image","02.jpg") //把它解压到当前目录下的image文件夹里,并命名为02.jpg
    tinify.fromFile(nowFilePath).toFile(filePositionPath)
    .then(()=>{
    console.log(nowFilePath + "解压完成")
    }).catch(err=>{
    console.log("解压失败:"+err);
    })
    //当然如果压缩多张图片也可以使用递归的方式去处理。如果图片数量多的话,压缩时间会有点慢,还有拓展的空间。
    

      

  • 相关阅读:
    LeetCode 1245. Tree Diameter
    LeetCode 1152. Analyze User Website Visit Pattern
    LeetCode 1223. Dice Roll Simulation
    LeetCode 912. Sort an Array
    LeetCode 993. Cousins in Binary Tree
    LeetCode 1047. Remove All Adjacent Duplicates In String
    LeetCode 390. Elimination Game
    LeetCode 1209. Remove All Adjacent Duplicates in String II
    LeetCode 797. All Paths From Source to Target
    LeetCode 1029. Two City Scheduling
  • 原文地址:https://www.cnblogs.com/kongyijilafumi/p/12814440.html
Copyright © 2011-2022 走看看