Create a tarball (tarball) using Coldfusion \ Java on a Windows server

I need to create a TAR file containing several files on a Windows server using Coldfusion \ Java. I found many examples of unpacking them, but very few created them. I found this example of using gzip to add some text to a file, and it works, but I need to add files. I'm also not 100% sure that gzip is the same as creating tarball. This project was assigned to me with a very short turn, and I spin my wheels, so any help in the right direction is much appreciated:

Win Server 2012, ColdFusion 10, Java Version 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() />
+4
source share
1

, tar gzip - . ZIP GZIP:

... tar ( ), (gzip compress) .

- 7-Zip ( tar 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 Apache Commons 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 - TAR

NB: , . . 3: Zip.java. , Zip. - , .

+2

Source: https://habr.com/ru/post/1674015/


All Articles