]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/PackageEditor/src/org/tianocore/packaging/CreateFdp.java
git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@671 6f19259b...
[mirror_edk2.git] / Tools / Source / PackageEditor / src / org / tianocore / packaging / CreateFdp.java
1 /** @file
2 Java class CreateFdp is used to create a distributable package containing
3 FDPManifest.xml file in its root directory.
4
5 Copyright (c) 2006, Intel Corporation
6 All rights reserved. This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13 **/
14 package org.tianocore.packaging;
15
16 import java.io.*;
17 import java.util.jar.*;
18
19 /**
20 This class contains static method create to generate *.fdp format package.
21
22 @since PackageEditor 1.0
23 **/
24 public class CreateFdp {
25
26 /**
27 recursively add contents under dir into output package.
28
29 @param dir The directory with files that will be put into package
30 @param jos Stream used to create output package
31 @param wkDir The position of source directory
32 @throws Exception Any exception occurred during this process
33 **/
34 public static void create(File dir, JarOutputStream jos, String wkDir) throws Exception {
35
36 String[] list = dir.list();
37
38 try {
39 byte[] buffer = new byte[1024];
40 int bytesRead;
41
42 //
43 // Loop through the file names provided.
44 //
45 for (int i = 0; i < list.length; i++) {
46
47 File f = new File(dir, list[i]);
48 if (f.getName().equals("..")) {
49 continue;
50 }
51 if (f.isDirectory()) {
52 //
53 // Call this method recursively for directory
54 //
55 CreateFdp.create(f, jos, wkDir);
56 continue;
57 }
58
59 try {
60 //
61 // Open the file
62 //
63 FileInputStream fis = new FileInputStream(f);
64
65 try {
66 //
67 // Create a Jar entry and add it, keep relative path only.
68 //
69 JarEntry entry = new JarEntry(f.getPath().substring(wkDir.length() + 1));
70 jos.putNextEntry(entry);
71
72 //
73 // Read the file and write it to the Jar.
74 //
75 while ((bytesRead = fis.read(buffer)) != -1) {
76 jos.write(buffer, 0, bytesRead);
77 }
78
79 System.out.println(entry.getName() + " added.");
80 } catch (Exception ex) {
81 System.out.println(ex);
82 } finally {
83 fis.close();
84 }
85 } catch (IOException ex) {
86 System.out.println(ex);
87 }
88 }
89 } finally {
90 System.out.println(dir.getPath() + " processed.");
91 }
92 }
93 }