当前位置: 代码网 > it编程>编程语言>Java > Java实现Jar文件的遍历复制与文件追加

Java实现Jar文件的遍历复制与文件追加

2024年12月01日 Java 我要评论
一、引入依赖<dependency><groupid>commons-io</groupid><artifactid>commons-io</ar

一、引入依赖

		<dependency>
			<groupid>commons-io</groupid>
			<artifactid>commons-io</artifactid>
			<version>2.5</version>
		</dependency>
		<!-- slf4j + log4j2 begin -->
		<dependency>
			<groupid>org.apache.logging.log4j</groupid>
			<artifactid>log4j-slf4j-impl</artifactid>
			<version>2.12.1</version>
		</dependency>
		<!-- log4j end -->
		<dependency>
			<groupid>org.projectlombok</groupid>
			<artifactid>lombok</artifactid>
			<version>1.18.12</version>
			<scope>provided</scope>
		</dependency> 
		<dependency>
			<groupid>org.apache.commons</groupid>
			<artifactid>commons-compress</artifactid>
			<version>1.23.0</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupid>junit</groupid>
			<artifactid>junit</artifactid>
			<version>4.12</version>
			<scope>test</scope>
		</dependency>

二、文件准备

三、java编码实现

import java.io.bufferedinputstream;
import java.io.bufferedoutputstream;
import java.io.file;
import java.io.fileinputstream;
import java.io.fileoutputstream;
import java.io.ioexception;
import java.io.inputstream;
import java.util.jar.jarentry;
import java.util.jar.jarinputstream;
import java.util.jar.jaroutputstream;

import org.apache.commons.io.ioutils;
import org.apache.commons.lang3.systemutils;
import org.apache.commons.lang3.time.dateformatutils;
import org.junit.test;

import lombok.extern.slf4j.slf4j;

/**
 * @author 00fly
 *
 */
@slf4j
public class jarentrytest
{
    /**
     * jdk-api实现-将addfiles添加到srcjar并重命名为newjar
     * 
     * @param srcjar
     * @param newjar
     * @param addfiles
     * @throws ioexception
     */
    private void addfilestojar(file srcjar, string newjar, file... addfiles)
        throws ioexception
    {
        try (jarinputstream jis = new jarinputstream(new fileinputstream(srcjar)); jaroutputstream jos = new jaroutputstream(new fileoutputstream(newjar), jis.getmanifest()))
        {
            jarentry jarentry;
            while ((jarentry = jis.getnextjarentry()) != null)
            {
                jos.putnextentry(jarentry);
                ioutils.copy(jis, jos);
            }
            
            // 追加文件
            for (file addfile : addfiles)
            {
                jarentry = new jarentry("add/" + addfile.getname());
                jos.putnextentry(jarentry);
                try (inputstream entryinputstream = new fileinputstream(addfile))
                {
                    ioutils.copy(entryinputstream, jos);
                }
            }
        }
    }
    
    /**
     * 拷贝 jar
     * 
     * @param fromjar
     * @param tojar
     * @throws ioexception
     */
    private void copyjar(string fromjar, string tojar)
        throws ioexception
    {
        try (jarinputstream jis = new jarinputstream(new bufferedinputstream(new fileinputstream(fromjar))); jaroutputstream jos = new jaroutputstream(new bufferedoutputstream(new fileoutputstream(tojar)), jis.getmanifest()))
        {
            jarentry je;
            while ((je = jis.getnextjarentry()) != null)
            {
                jos.putnextentry(je);
                int iread;
                while ((iread = jis.read()) != -1)
                {
                    jos.write(iread);
                }
            }
            jis.closeentry();
            jos.finish();
        }
    }
    
    @test
    public void testaddfiles()
    {
        try
        {
            string src = getclass().getresource("/apache-jstl.jar").getpath();
            string add1 = getclass().getresource("/servlet-api.jar").getpath();
            string add2 = getclass().getresource("/log4j2.xml").getpath();
            string newjar = src.replace(".jar", dateformatutils.format(system.currenttimemillis(), "_hhmmsssss") + ".jar");
            log.info("源文件: {}", src);
            log.info("++新增: {}", add1);
            log.info("++新增: {}", add2);
            log.info("新文件: {}", newjar);
            addfilestojar(new file(src), newjar, new file(add1), new file(add2));
        }
        catch (ioexception e)
        {
            log.error(e.getmessage(), e);
        }
    }
    
    @test
    public void testcopy()
    {
        try
        {
            string src = getclass().getresource("/servlet-api.jar").getpath();
            string newjar = src.replace(".jar", dateformatutils.format(system.currenttimemillis(), "_hhmmsssss") + ".jar");
            log.info("源文件: {}", src);
            log.info("新文件: {}", newjar);
            copyjar(src, newjar);
            
            if (systemutils.is_os_windows)
            {
                runtime.getruntime().exec("cmd /c start " + new file(newjar).getparentfile().getcanonicalpath());
            }
        }
        catch (ioexception e)
        {
            log.error(e.getmessage(), e);
        }
    }
    
    /**
     * 遍历打印
     * 
     * @throws ioexception
     */
    @test
    public void testlist()
        throws ioexception
    {
        string src = getclass().getresource("/servlet-api.jar").getpath();
        try (jarinputstream jis = new jarinputstream(new bufferedinputstream(new fileinputstream(src))))
        {
            jarentry je;
            while ((je = jis.getnextjarentry()) != null)
            {
                system.out.println(je.getname());
            }
            jis.closeentry();
        }
    }
}

四、compress编码实现

compress 功能非常强大,可以参考官网样例:

https://commons.apache.org/proper/commons-compress/examples.html

import java.io.fileinputstream;
import java.io.fileoutputstream;
import java.io.ioexception;
import java.io.inputstream;
import java.nio.charset.standardcharsets;

import org.apache.commons.compress.archivers.archiveoutputstream;
import org.apache.commons.compress.archivers.jar.jararchiveentry;
import org.apache.commons.compress.archivers.jar.jararchiveinputstream;
import org.apache.commons.compress.archivers.jar.jararchiveoutputstream;
import org.apache.commons.io.ioutils;
import org.apache.commons.lang3.stringutils;
import org.apache.commons.lang3.time.dateformatutils;
import org.junit.test;

import lombok.extern.slf4j.slf4j;

@slf4j
public class compresstest
{
    @test
    public void test()
        throws ioexception
    {
        string src = getclass().getresource("/apache-jstl.jar").getpath();
        string add1 = getclass().getresource("/servlet-api.jar").getpath();
        string add2 = getclass().getresource("/log4j2.xml").getpath();
        string newjar = src.replace(".jar", dateformatutils.format(system.currenttimemillis(), "_hhmmsssss") + ".jar");
        log.info("源文件: {}", src);
        log.info("++新增: {}", add1);
        log.info("++新增: {}", add2);
        log.info("新文件: {}", newjar);
        
        try (jararchiveinputstream jarinput = new jararchiveinputstream(new fileinputstream(src)); archiveoutputstream outputstream = new jararchiveoutputstream(new fileoutputstream(newjar)))
        {
            jararchiveentry jarentry;
            while ((jarentry = jarinput.getnextjarentry()) != null)
            {
                outputstream.putarchiveentry(jarentry);
                ioutils.copy(jarinput, outputstream);
            }
            outputstream.flush();
            
            // 追加addfiles
            string[] addfiles = {add1, add2};
            for (string addfile : addfiles)
            {
                jararchiveentry addentry = new jararchiveentry("add/" + stringutils.substringafterlast(addfile, "/"));
                outputstream.putarchiveentry(addentry);
                try (inputstream entryinputstream = new fileinputstream(addfile))
                {
                    ioutils.copy(entryinputstream, outputstream);
                }
            }
            
            // 追加add/001.txt
            jararchiveentry entry = new jararchiveentry("add/001.txt");
            outputstream.putarchiveentry(entry);
            outputstream.write("org.apache.commons.compress.archivers.jar.jararchiveoutputstream;".getbytes(standardcharsets.utf_8));
            outputstream.closearchiveentry();
            outputstream.finish();
        }
    }
}

到此这篇关于java实现jar文件的遍历复制与文件追加的文章就介绍到这了,更多相关java jar文件操作内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com