在Windows服务器上使用Coldfusion \ Java创建一个TAR文件(tarball)

问题描述:

我需要使用Coldfusion \ Java在Windows服务器上创建一个包含多个文件的TAR文件。我发现了很多拆包的例子,但创建它们却很少。我发现这个使用gzip向文件添加一些文本的例子,它可以工作,但我需要添加文件。我也不是100%肯定gzip和构建tarball是一样的。该项目分配给我用很短的周转和我纺纱我的车轮,从而在正确的方向上没有任何帮助是极大的赞赏:在Windows服务器上使用Coldfusion Java创建一个TAR文件(tarball)

赢Server 2012中, 的ColdFusion 10, Java版本1.7.0_15

<cfset lineBreak = chr(13) & chr(10) /> 
<!--- open the sitemap file ---> 
<cfset tarFilePath = "#application.imageingFolder#DTSimages\Pending\tiff.gz" /> 
#tarFilePath# 
<!--- create streams ---> 
<cfset outputStream = CreateObject("java", "java.io.FileOutputStream").Init(
      CreateObject("java","java.io.File").Init(tarFilePath)) /> 
<cfset gzipStream = CreateObject("java", "java.util.zip.GZIPOutputStream").Init(outputStream) /> 
<cfsavecontent variable="siteMapHeader"><?xml version="1.0" encoding="UTF-8"?> 
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" 
    xmlns:image="http://www.sitemaps.org/schemas/sitemap-image/1.1" 
    xmlns:video="http://www.sitemaps.org/schemas/sitemap-video/1.1"> 
</cfsavecontent> 
<cfset siteMapFooter = "</urlset>" /> 
<cfset gzipStream.write(ToString(siteMapHeader).GetBytes()) /> 

<cfset gzipStream.close() /> 
<cfset outputStream.close() /> 

虽然often used together,tar和gzip是不同的东西。从ZIP vs. GZIP

... tar命令用来创建一个档案(不 压缩)和其他程序(的gzip或压缩)用于压缩 存档。

一个简单的选择是安装7-Zip(支持焦油和gzip)中,用适当的参数从cfexecute调用它。要创建一个TAR文件(未压缩):

<!--- 
    "a" - add files to archive 
    "t" - Type of archive 
    output .tar file 
    space separated list of files to add 
---> 
<cfexecute name="c:\Program Files\7-Zip\7z.exe" 
    arguments=" a -ttar c:\path\myarchive.tar c:\path\file1.xlsx c:\temp\otherfile.txt" 
    variable="output" 
    errorVariable="error" 
    timeout="60" /> 

<cfoutput> 
    output = #output#<br> 
    error = #error#<br> 
</cfoutput> 

对于Java选项,this thread提到使用Apache共享库,用于创建TAR文件。这也恰好与CF捆绑所以它应该工作开箱:

http://www.oracle.com/technetwork/articles/java/compress-1565076.html

<cfscript> 
    // Initialize TAR file to generate 
    outputPath = "c:/temp/outputFile3.tar"; 
    os = createObject("java", "java.io.FileOutputStream").init(outputPath); 
    tar = createObject("java", "org.apache.commons.compress.archivers.tar.TarArchiveOutputStream").init(os); 

    // Add an entry from a string 
    someTextContent = '<?xml version="1.0" encoding="UTF-8"?>....'; 
    binaryContent = charsetDecode(someTextContent, "utf-8"); 
    entry = createObject("java", "org.apache.commons.compress.archivers.tar.TarArchiveEntry").init("siteHeader.xml"); 
    entry.setSize(arrayLen(binaryContent)); 
    tar.putArchiveEntry(entry); 
    tar.write(binaryContent); 
    tar.closeArchiveEntry(); 

    // Create an entry from a file 
    inputFile = createObject("java", "java.io.File").init("c:/path/someImage.jpg"); 
    entry = tar.createArchiveEntry(inputFile, "myImage.jpg"); 
    tar.putArchiveEntry(entry); 
    tar.write(FileReadBinary(inputFile)); 
    tar.closeArchiveEntry(); 

    // Close TAR file 
    tar.flush(); 
    tar.close(); 
</cfscript> 

详情请参见文档:Apache Commons - The TAR package

注意:如果你正在归档大文件,研究缓冲。有关基本概念,请参阅Code Sample 3: Zip.java。忽略它是用于Zip文件的事实。基本概念是一样的,只有不同的类。

+0

工作正常!我一直在尝试jTar,但没有太多的运气,正准备尝试这个。当我用7-zip打开TAR文件时出现一个奇怪的错误: – RobE

+0

错误是“存档结束后有数据”。我不确定那是什么。 – RobE

+0

使用哪个例子?在我的(简短)测试中,对于我来说,在7zip中都没有错误地打开。 – Leigh