欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java實現(xiàn)Jar文件的遍歷復(fù)制與文件追加

 更新時間:2024年11月30日 15:15:08   作者:愛碼少年 00fly.online  
這篇文章主要為大家詳細介紹了如何利用Java實現(xiàn)Jar文件的遍歷復(fù)制與文件追加功能,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

一、引入依賴

		<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編碼實現(xiàn)

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實現(xiàn)-將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編碼實現(xiàn)

Compress 功能非常強大,可以參考官網(wǎng)樣例:

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();
        }
    }
}

到此這篇關(guān)于Java實現(xiàn)Jar文件的遍歷復(fù)制與文件追加的文章就介紹到這了,更多相關(guān)Java Jar文件操作內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • MyBatis的動態(tài)SQL語句實現(xiàn)

    MyBatis的動態(tài)SQL語句實現(xiàn)

    這篇文章主要介紹了MyBatis的動態(tài)SQL語句實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-01-01
  • Mybatis-plus數(shù)據(jù)權(quán)限D(zhuǎn)ataPermissionInterceptor實現(xiàn)

    Mybatis-plus數(shù)據(jù)權(quán)限D(zhuǎn)ataPermissionInterceptor實現(xiàn)

    本文主要介紹了Mybatis-plus數(shù)據(jù)權(quán)限D(zhuǎn)ataPermissionInterceptor實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • java11新特性之集合轉(zhuǎn)換為數(shù)組的方法

    java11新特性之集合轉(zhuǎn)換為數(shù)組的方法

    Java11引入了一種將帶有泛型的集合轉(zhuǎn)換為帶有泛型的數(shù)組的簡單方法,本文通過實例代碼介紹java11新特性之集合轉(zhuǎn)換為數(shù)組的操作方法,感興趣的朋友跟隨小編一起看看吧
    2024-06-06
  • 初識Spring boot監(jiān)控

    初識Spring boot監(jiān)控

    這篇文章主要介紹了spring boot監(jiān)控的相關(guān)知識,文中給大家介紹了查看監(jiān)控數(shù)據(jù),數(shù)據(jù)可視化的相關(guān)知識,需要的朋友可以參考下
    2018-03-03
  • 代理模式:JAVA靜態(tài)代理和動態(tài)代理的實例和實現(xiàn)詳解

    代理模式:JAVA靜態(tài)代理和動態(tài)代理的實例和實現(xiàn)詳解

    這篇文章主要給大家介紹了關(guān)于Java靜態(tài)代理和動態(tài)代理的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-08-08
  • Java使用html2image將html生成縮略圖圖片的實現(xiàn)示例

    Java使用html2image將html生成縮略圖圖片的實現(xiàn)示例

    本文主要介紹了Java使用html2image將html生成縮略圖圖片的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-12-12
  • 詳解pom如何引入非Maven工程的jar包

    詳解pom如何引入非Maven工程的jar包

    系統(tǒng)遷移從某個公有云遷移到私有云,因為現(xiàn)在國內(nèi)大力推行國產(chǎn)化,所以我們這次遷移有兩個國產(chǎn)化的東西,第一個是操作系統(tǒng)采用了歐拉操作系統(tǒng),第二個就是數(shù)據(jù)庫采用了goldendb,本文給大家詳細介紹了pom如何引入非Maven工程的jar包,需要的朋友可以參考下
    2023-12-12
  • SpringBoot中的自定義Starter解讀

    SpringBoot中的自定義Starter解讀

    這篇文章主要介紹了SpringBoot中的自定義Starter解讀,啟動器模塊其實是一個空的jar文件,里面沒有什么類、接口,僅僅是提供輔助性依賴管理,這些依賴可能用于自動裝配或者其他類庫,需要的朋友可以參考下
    2023-12-12
  • Java中對話框的彈出方法

    Java中對話框的彈出方法

    下面小編就為大家?guī)硪黄狫ava中對話框的彈出方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-10-10
  • java中常用的字符串的比較方法(兩種)

    java中常用的字符串的比較方法(兩種)

    本文主要介紹了java中兩種常用的字符串的比較方法。具有很好的參考價值。下面跟著小編一起來看下吧
    2017-03-03

最新評論