如何用nodejs压缩文件,生成tar包
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何用nodejs压缩文件,生成tar包相关的知识,希望对你有一定的参考价值。
参考技术A 调用shell的tar命令生成压缩包java 如何用zlib解压缩tar.gz文件
参考技术A public static void makeZip(List<File> fileList,String zipPath,boolean isDelete)byte[] buf = new byte[1024];
try
// Create the ZIP file
File zipFile = new File(zipPath);
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile));
// Compress the files
for (int i = 0; i < fileList.size(); i++)
FileInputStream in = new FileInputStream(fileList.get(i));
// Add ZIP entry to output stream.
out.putNextEntry(new ZipEntry(fileList.get(i).getName()));
// Transfer bytes from the file to the ZIP file
int len;
while ( (len = in.read(buf)) > 0)
out.write(buf, 0, len);
// Complete the entry
out.closeEntry();
in.close();
// Complete the ZIP file
out.close();
System.out.println("压缩完成.");
//把旧的文件删除
if(isDelete == true)
for (int i = 0; i < fileList.size(); i++)
File oldFile = fileList.get(i);
oldFile.delete();
catch (IOException e)
e.printStackTrace();
public static void main(String[] args)
File in1=new File("D:\\a.txt");
File in2=new File("D:\\b.txt");
File[] file=new File[]in1,in2;
File zip=new File("D:\\ab.zip");
IDMZip mgr=new IDMZip();
mgr.ZipFiles(file, zip);
这个方法不管你是在windows下还是在linux下,都能正常执行。追问
谢谢,但是我是要解压这样20140718_185819.data.tar.gz 不是zip的
追答你可以试试。还有这个方法。都是我项目里曾经用到过的。都是可用的。
public static List unZip(String path) throws FileNotFoundException,
IOException
path = path.replaceAll("\\\\","/");
String zipPath = path.substring(0,path.lastIndexOf("/")+1);
List xmlFileNameList = new ArrayList();
byte[] data = new byte[1024*2];
FileInputStream fis = new FileInputStream(path);
ZipInputStream zis = new ZipInputStream(fis);
ZipEntry entry = null;
while((entry = zis.getNextEntry())!= null)
if(entry.isDirectory())
File file = new File(zipPath+entry.getName());
file.mkdirs();
continue;
以上是关于如何用nodejs压缩文件,生成tar包的主要内容,如果未能解决你的问题,请参考以下文章