空のjarファイルエントリを作成しています

 

/**
 *
 */
package jarutils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarOutputStream;
import java.util.zip.ZipEntry;

/**
 * @author dw-wang
 *
 */
public final class JarUtils
              {
                  private static final int BUFFER_SIZE = 4096;
              
                  /**
                   * Utility class - no instances allowed.
                   */
                  private JarUtils ()
                  {
                      // no instances allowed -- provides only static helper functions
                  }
              
                  /**
                   * Extract a jar archive into the base directory
                   * <code>baseDir</code>.
                   *
                   * @param baseDir the root directory where the archive is extracted
                   *        to.
                   * @param archive jar file.
                   * @throws IOException in case of an I/O error.
                   */
                  public static void extractJarArchive (File baseDir, File archive)
                      throws IOException
                  {
                      final JarFile archiveFile = new JarFile(archive);
                      final List archiveEntries = Collections.list(archiveFile.entries());
                      for (final Iterator iterator = archiveEntries.iterator(); iterator
                          .hasNext();)
                      {
                          InputStream in = null;
                          FileOutputStream out = null;
                          try
                          {
                              final ZipEntry e = (ZipEntry) iterator.next();
                              in = archiveFile.getInputStream(e);
                              final File f = new File(baseDir, e.getName());
                              if (e.isDirectory())
                              {
                                  if (!f.exists())
                                  {
                                      if (!f.mkdirs())
                                      {
                                          throw new IOException("Cannot create directory "
                                              + f);
                                      }
                                  }
                              }
                              else
                              {
                                  if (!f.getParentFile().exists())
                                  {
                                      if (!f.getParentFile().mkdirs())
                                      {
                                          throw new IOException("Cannot create directory "
                                              + f.getParentFile());
                                      }
                                  }
                                  out = new FileOutputStream(f);
                                  copy(in, out);
                              }
                          }
                          finally
                          {
                              IoUtil.close(out);
                              IoUtil.close(in);
                          }
                      }
                  }
              
                  /**
                   * Creates a jar archive from the directory.
                   *
                   * @param baseDir for the jar archive.
                   * @param archive the jar file.
                   * @throws IOException in case of an I/O error.
                   */
                  public static void createJarArchive (File baseDir, File archive)
                      throws IOException
                  {
                      JarOutputStream jarArchive = null;
                      try
                      {
                          jarArchive = new JarOutputStream(new FileOutputStream(archive));
                          addFileToJar(baseDir, baseDir, jarArchive);
                      }
                      finally
                      {
                          IoUtil.close(jarArchive);
                      }
                  }
              
                  private static void addFileToJar (File baseDir, File file,
                      JarOutputStream archive)
                      throws IOException
                  {
                      if (file == null)
                      {
                          // done
                      }
                      else if (file.isDirectory())
                      {
                          String path = FileUtils.getRelativePath(baseDir, file);
     (1)(2)(3)(4)   if(!(3)path.equals("/") && !(4)path.equals(""))
                          {
                              if (!path.endsWith("/"))
                              {
     (5)                       path (5)+= "/";
                              }
                              final JarEntry entry = new JarEntry(path);
                              archive.putNextEntry(entry);
     (6)                      archive.closeEntry();
                          }
                          final File[] files = file.listFiles();
                          for (int i = 0; i < files.length; i++)
                          {
                              addFileToJar(baseDir, files[i], archive);
                          }
                      }
                      else
                      {
                          final String path = FileUtils.getRelativePath(baseDir, file);
                          final JarEntry entry = new JarEntry(path);
                          archive.putNextEntry(entry);
                          InputStream in = null;
                          try
                          {
                              in = new FileInputStream(file);
                              copy(in, archive);
                          }
                          finally
                          {
                              IoUtil.close(in);
                          }
                          archive.closeEntry();
                      }
                  }
              
                  /**
                   * Copies the content of the input stream <code>in</code> to the
                   * output stream <code>out</code>.
                   *
                   * @param in input stream.
                   * @param out output stream.
                   * @throws IOException in case of an I/O error.
                   */
                  private static void copy (InputStream in, OutputStream out)
                      throws IOException
                  {
                      final byte[] buffer = new byte[BUFFER_SIZE];
                      int nread;
                      while ((nread = in.read(buffer)) != -1)
                      {
                          out.write(buffer, 0, nread);
                      }
                  }
          }

 

 

Findings in this File

i (1) 159 : 0 method org.jcoderz.commons.util.JarUtils.addFileToJar(File, File, JarOutputStream) makes literal string comparisons passing the literal as an argument
i (2) 159 : 0 method org.jcoderz.commons.util.JarUtils.addFileToJar(File, File, JarOutputStream) makes literal string comparisons passing the literal as an argument
c (3) 159 : 18 Position literals first in String comparisons
c (4) 159 : 39 Position literals first in String comparisons
c (5) 163 : 26 Prefer StringBuffer over += for concatenating strings
w (6) 167 : 0 Empty jar file entry created in org.jcoderz.commons.util.JarUtils.addFileToJar(File, File, JarOutputStream)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值