Subversion Repositories display

Rev

Rev 425 | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*
 * Copyright (C) 2007 Steve Ratcliffe
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *
 * Author: Steve Ratcliffe
 * Create date: 20-Oct-2007
 */

package test;

import uk.me.parabola.imgfmt.FileSystemParam;
import uk.me.parabola.imgfmt.Sized;
import uk.me.parabola.imgfmt.fs.FileSystem;
import uk.me.parabola.imgfmt.fs.ImgChannel;
import uk.me.parabola.imgfmt.sys.FileImgChannel;
import uk.me.parabola.imgfmt.sys.FileLink;
import uk.me.parabola.imgfmt.sys.ImgFS;
import uk.me.parabola.imgfmt.Utils;

import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;

/**
 * Combines several files into one .img file.
 * Kind of the opposite of ExtractFile.
 *
 * It can be used to reconstitute a tile .img file from the .LBL/.TRE/.RGN/.NET/.NOD etc files
 * so that other display diagnostics can be run.
 * eg, starting with just gmapsupp.img, extract all the components:
 *  $ doDisplay test.ExtractFile gmapsupp.img
 * or, from a --gmapi directory, the components are in $family-name.gmap/Product1/${tilename}
 * then, for either:
 *  $ doDisplay test.ZipFile ${tilename}.img ${tilename}.*
 * now you can the various checks and displays in the tile, eg:
 *  $ doDisplay test.check.NodCheck ${tilename}.img
 *
 * @author Steve Ratcliffe
 */

public class ZipFile {
        public static void main(String[] args) throws IOException {
                FileSystemParam params = new FileSystemParam();

                boolean first = true;
                String outfile = "63240001.img";
                List<String> inlist = new ArrayList<>();
                for (String arg : args) {
                        if (arg.startsWith("--block-size")) {
                                int bsize = Integer.parseInt(arg.substring(13));
                                params.setBlockSize(bsize);
                        } else if (first) {
                                outfile = arg;
                                first = false;
                        } else {
                                inlist.add(arg);
                        }
                }

                FileSystem fs = ImgFS.createFs(outfile, params);

                for (String name : inlist) {
                        System.out.println("file " + name);
                        File f = new File(name);
                        FileCopier fc = new FileCopier(name);
                        ImgChannel fout = fs.create(f.getName().toUpperCase());
                        ((FileLink)fout).link(fc, fc.file(fout));
                }

                Utils.closeFile(fs);
        }
}

/**
 * Copies file to composite .img.
 *
 * based on similar in mkgmap/builders/GmapsuppBuilder.java
 */

class FileCopier implements Sized {
        private final String filename;
        private long fileSize;

        public FileCopier(String filename) {
                this.filename = filename;
                File f = new File(filename);
                fileSize = f.length();
        }

        Closeable file(ImgChannel fout) {
                return () -> sync(fout);
        }

        public void sync(ImgChannel fout) throws IOException {
                try (ImgChannel fin = new FileImgChannel(filename, "r")) {
                        copyFile(fin, fout);
                }
        }

        private static void copyFile(ImgChannel fin, ImgChannel fout) throws IOException {
                ByteBuffer buf = ByteBuffer.allocate(1024);
                while (fin.read(buf) > 0) {
                        buf.flip();
                        fout.write(buf);
                        buf.compact();
                }
        }

        @Override
        public long getSize() {
                return fileSize;
        }

}