protected void copyFileToFile(File source, File destination) throws IOException { if (!source.exists()) { throw new IOException( "The file '" + source + "' does not exist (tried to link to '" + destination + "')."); } if (destination.exists()) { throw new IOException("The file '" + destination + "' already exists."); } if (source.isDirectory()) { if (!destination.mkdirs()) { throw new IOException("Failed to create the directory '" + destination + "'."); } for (File child : source.listFiles()) { copyFileToFile(child, new File(destination, child.getName())); } } else { FileInputStream fis = new FileInputStream(source); ERXFileUtilities.writeInputStreamToFile( fis, destination, (int) source.length(), new NullProgressMonitor()); } }
public void extractTo( File destinationFolder, boolean symbolicLinksSupported, IProgressMonitor progressMonitor) throws IOException, InterruptedException { long amount = 0; List<Link> links = new LinkedList<Link>(); try { byte[] sixBuffer = new byte[6]; byte[] elevenBuffer = new byte[11]; boolean done = false; do { String magic = readString(paxStream, sixBuffer); if (!"070707".equals(magic)) { throw new IOException( "Expected magic '070707' but got '" + magic + "' (next = " + readString(paxStream, new byte[50]) + ")."); } else { String dev = readString(paxStream, sixBuffer); String ino = readString(paxStream, sixBuffer); String modeStr = readString(paxStream, sixBuffer); String uid = readString(paxStream, sixBuffer); String gid = readString(paxStream, sixBuffer); String nlink = readString(paxStream, sixBuffer); String rdev = readString(paxStream, sixBuffer); String mtime = readString(paxStream, elevenBuffer); String nameSizeStr = readString(paxStream, sixBuffer); String fileSizeStr = readString(paxStream, elevenBuffer); int nameSize = Integer.parseInt(nameSizeStr, 8); String name = readString(paxStream, new byte[nameSize]); int fileSize = Integer.parseInt(fileSizeStr, 8); if ("TRAILER!!!".equals(name)) { done = true; } else { File destinationFile = toFile(destinationFolder, name); int mode = Integer.parseInt(modeStr, 8); if ((mode & S_IFDIR) == S_IFDIR) { if (".".equals(name)) { // skip } else if (destinationFile.exists()) { throw new IOException("The directory '" + destinationFile + "' already exists."); } else if (!destinationFile.mkdirs()) { throw new IOException("Failed to create directory '" + destinationFile + "'."); } skipFully(paxStream, fileSize); } else if ((mode & S_IFLNK) == S_IFLNK) { String realName = readString(paxStream, new byte[fileSize]); File realFile = new File(realName); if (!symbolicLinksSupported) { realFile = toFile(destinationFile.getParentFile(), realName); } links.add(new Link(realFile, destinationFile)); } else if ((mode & S_IFREG) == S_IFREG) { if (destinationFile.exists()) { throw new IOException("The file '" + destinationFile + "' already exists."); } InputStream is = new BoundedInputStream(paxStream, 0, fileSize); FileOutputStream fos = new FileOutputStream(destinationFile); ERXFileUtilities.writeInputStreamToOutputStream( is, fos, fileSize, new NullProgressMonitor()); } else { throw new IOException("Unknown mode " + modeStr + " for " + name + "."); } amount += 70 + nameSize + fileSize; progressMonitor.progress(amount, fileLength); } } } while (!done); } finally { // System.out.println(amount + ":" + fileLength); paxStream.close(); } Collections.sort(links, new LinkNameLengthComparator()); int linkNum = 0; for (Link link : links) { link.create(symbolicLinksSupported); progressMonitor.progress(linkNum++, links.size()); } progressMonitor.done(); }