node複製資料夾,壓縮zip,上傳

2022-09-03 11:54:11 字數 1689 閱讀 3311

一:用到的模組和簡單介紹 npm文件;

request :node請求的模組,可以給用程式請求伺服器的介面

fs:讀寫檔案的,很常用

path:路徑,各種給資料夾弄路徑什麼的

js-cookie:獲取cokie的,在請求的時候需要用到

node-native-zip:壓縮zip的模組

//公共引入模組;

const path = require("path");

const fs = require("fs");

var request = require("request");

var zip = require("node-native-zip");

二:複製資料夾;

第一步複製:

由於zip結構包的需要 所以我需要的複製的最後路徑為

**實現如下:

var pathrr=localpath.split('\\');

//注:這裡用了js的拆分字串的方法獲取路徑,很麻煩,可以直接用path模組的方法

//拷貝專案目錄到臨時目錄中

if (fs.existssync(containerdir0)) else

}if (fs.existssync(containerdir)) else

}fs.mkdirsync(containerdir0);//建立目錄

fs.mkdirsync(containerdir);//建立目錄

let copy = function(src, dst) else

});};let exists = function(src, dst, callback) else

};let copytree = function(src, dst) else

} fs.mkdirsync(targetdir);

copy(src, targetdir);

};copytree(localpath, containerdir);

function deletefolderrecursive(path) else

});fs.rmdirsync(path);

}}//拷貝專案目錄到臨時目錄中 end

這裡複製就完成了,而且這個也複製了隱藏的資料夾和下面的檔案

第二步:壓縮zip

//壓縮臨時目錄

//要壓縮資料夾所在的父目錄

let pathtozipdir = path.dirname(containerdir); //'e:183svn/123456789/';

//要壓縮資料夾所在的最後一層目錄

(function () );

} else

})(path.join(pathtozipdir, dirtozip));

let buff = archive.tobuffer();

fs.writefile(outputpath, buff, function () /api2/upload`,

method:"post",

formdata: ,

headers:

},function (err, response, body) else

})});

}());

//壓縮臨時目錄end

java 壓縮資料夾 zip

purpose 壓縮資料夾 author hermanwang param srcdir 需要壓縮的檔案的路徑 param out zip輸出流 param keepdirstructure 是否保留原來的目錄結構,true 保留目錄結構 false 所有檔案跑到壓縮包根目錄下 注意 不保留目錄結構...

node 資料夾建立

建立資料夾 var fs require fs 1.非同步 fs.mkdir 第乙個目錄 function err console.log 第乙個目錄目錄建立成功。fs.mkdir 第乙個目錄 test1 function err console.log test1目錄建立成功。fs.mkdir 第...

檔案 資料夾複製

通道對通道複製效率高 param f1 原始檔 param f2 目的檔案 return throws exception public static long forchannel file f1,file f2 throws exception if inc.size inc.position ...